473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Function Parameter passing? Session? Static? Global?

Dear Newsgroupers,

The 'main' page contains a call to a function in an included file. This
function puts a html-form on the screen. Before the form gets posted (to the
'main' page) some prior errorchecking is done. The form fields are then sent
and an object is created (all in the same function).
What I need ( imo ) is an array with objects that can be written later to a
dbfile to preserve.

But I cannot find a solution for the extension of the array.

the function statements are simply these:

< inside function in included file >
$o = someObject( $s ) ;
$objarr[] = $o;
</ inside function in included file >

As this array is within a function it is local to this function.
As I understand it now a good solution would be to pass the array to the
function.
In the 'main' page I made a variable $objarr that is passed into the
function like this:
<'main' page>
$objarr ;
function( $objarr )
</'main' page>

The PHP manual states that in the absence of the array variable it gets
created. {Does this mean that function( $objarr ) would suffice? }
However,
whatever I try, I end up with the creation of $objarr[ 0 ] only.

Please advise

TIA,

pablo
Jul 17 '05 #1
6 2229
pablo wrote:
What I need ( imo ) is an array with objects that can be written later to a
dbfile to preserve.

But I cannot find a solution for the extension of the array.

the function statements are simply these:

< inside function in included file >
$o = someObject( $s ) ;
$objarr[] = $o;
</ inside function in included file >

As this array is within a function it is local to this function.
As I understand it now a good solution would be to pass the array to the
function.
In the 'main' page I made a variable $objarr that is passed into the
function like this:
<'main' page>
$objarr ;
function( $objarr )
</'main' page>

The PHP manual states that in the absence of the array variable it gets
created. {Does this mean that function( $objarr ) would suffice? }
However,
whatever I try, I end up with the creation of $objarr[ 0 ] only.

Please advise

Pass the function parameter by reference:

pedro$ cat xx.php
<?php
function more_array(&$arr) {
// reference _____^_____
$arr[] = 'one';
$arr[] = 'two';
}

more_array($x);
print_r($x);
?>

pedro$ php xx.php
Array
(
[0] => one
[1] => two
)

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #2

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
and gave some answer:
pablo wrote:
What I need ( imo ) is an array with objects that can be written later to a dbfile to preserve.

But I cannot find a solution for the extension of the array.

the function statements are simply these:

< inside function in included file >
$o = someObject( $s ) ;
$objarr[] = $o;
</ inside function in included file >

As this array is within a function it is local to this function.
As I understand it now a good solution would be to pass the array to the
function.
In the 'main' page I made a variable $objarr that is passed into the
function like this:
<'main' page>
$objarr ;
function( $objarr )
</'main' page>

The PHP manual states that in the absence of the array variable it gets
created. {Does this mean that function( $objarr ) would suffice? }
However,
whatever I try, I end up with the creation of $objarr[ 0 ] only.

Please advise

Pass the function parameter by reference:

pedro$ cat xx.php
<?php
function more_array(&$arr) {
// reference _____^_____
$arr[] = 'one';
$arr[] = 'two';
}

more_array($x);
print_r($x);
?>

pedro$ php xx.php
Array
(
[0] => one
[1] => two
)

First of all thanks for the answer, but I find it difficult to understand.
I tried to follow your example but it doesn't solve my problem.
Your array $x seems to exist already outside the function.
As I see it I can go about in this way:
$x[] = "three";
more_array( $x ) ;

And I suspect the function is not necessary.

But what I want to add to the array is contained and created inside the
function.

pablo

Jul 17 '05 #3
pablo wrote:

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
and gave some answer:
pablo wrote:
> What I need ( imo ) is an array with objects that can be written later to a > dbfile to preserve.
>
> But I cannot find a solution for the extension of the array.
>
> the function statements are simply these:
>
> < inside function in included file >
> $o = someObject( $s ) ;
> $objarr[] = $o;
> </ inside function in included file >
>
> As this array is within a function it is local to this function.
> As I understand it now a good solution would be to pass the array to the
> function.
> In the 'main' page I made a variable $objarr that is passed into the
> function like this:
> <'main' page>
> $objarr ;
> function( $objarr )
> </'main' page>
>
> The PHP manual states that in the absence of the array variable it gets
> created. {Does this mean that function( $objarr ) would suffice? }
> However,
> whatever I try, I end up with the creation of $objarr[ 0 ] only.
>
> Please advise

Pass the function parameter by reference:

pedro$ cat xx.php
<?php
function more_array(&$arr) {
// reference _____^_____
$arr[] = 'one';
$arr[] = 'two';
}

more_array($x);
print_r($x);
?>

pedro$ php xx.php
Array
(
[0] => one
[1] => two
)

First of all thanks for the answer, but I find it difficult to understand.
I tried to follow your example but it doesn't solve my problem.
Your array $x seems to exist already outside the function.
It is created right before the more_array() function is called.

Right before the script gets to the line
more_array($x);

$x is undefined.
For PHP to be able to call the function, because the function expects a
reference, the parameter *must* exist, so it is created right there.
As I see it I can go about in this way:
$x[] = "three";
If $x was undefined before last line, it is now an array with an element
with index 0 which holds the string "three".
more_array( $x ) ;
Calling print_r($x) now would output
[0] => three
[1] => one
[2] => two
And I suspect the function is not necessary.
??????
But what I want to add to the array is contained and created inside the
function.


Right! That is why you should use references :)
http://www.php.net/manual/en/language.references.php

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #4

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
pablo wrote:

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
and gave some answer:
pablo wrote:
> What I need ( imo ) is an array with objects that can be written later
to a
> dbfile to preserve.
>
> But I cannot find a solution for the extension of the array.
>
> the function statements are simply these:
>
> < inside function in included file >
> $o = someObject( $s ) ;
> $objarr[] = $o;
> </ inside function in included file >
>
> As this array is within a function it is local to this function.
> As I understand it now a good solution would be to pass the array to
the > function.
> In the 'main' page I made a variable $objarr that is passed into the
> function like this:
> <'main' page>
> $objarr ;
> function( $objarr )
> </'main' page>
>
> The PHP manual states that in the absence of the array variable it gets > created. {Does this mean that function( $objarr ) would suffice? }
> However,
> whatever I try, I end up with the creation of $objarr[ 0 ] only.
>
> Please advise
Pass the function parameter by reference:

pedro$ cat xx.php
<?php
function more_array(&$arr) {
// reference _____^_____
$arr[] = 'one';
$arr[] = 'two';
}

more_array($x);
print_r($x);
?>

pedro$ php xx.php
Array
(
[0] => one
[1] => two
)

First of all thanks for the answer, but I find it difficult to

understand. I tried to follow your example but it doesn't solve my problem.
Your array $x seems to exist already outside the function.


It is created right before the more_array() function is called.

Right before the script gets to the line
more_array($x);

$x is undefined.
For PHP to be able to call the function, because the function expects a
reference, the parameter *must* exist, so it is created right there.
As I see it I can go about in this way:
$x[] = "three";


If $x was undefined before last line, it is now an array with an element
with index 0 which holds the string "three".
more_array( $x ) ;


Calling print_r($x) now would output
[0] => three
[1] => one
[2] => two
And I suspect the function is not necessary.


??????
But what I want to add to the array is contained and created inside the
function.


Right! That is why you should use references :)
http://www.php.net/manual/en/language.references.php

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |


I think I can smile as well now ;).
Reworked your code to:
function more_array( &$arr ) {
static $n = 0 ;
$cnt = count( $arr) +1 ;
$t = "$n. this text. Array contains $cnt element(s). " ;
$arr[] = $t ;
$n++ ;
}
And that did wonders to understand what is going on.

So, along this route I reworked my own code....
BUT in my code I still see only item 0 gets created and the count reveals
only this item?


Jul 17 '05 #5
pablo wrote:
I think I can smile as well now ;).
Good!
Reworked your code to:
function more_array( &$arr ) {
static $n = 0 ;
$cnt = count( $arr) +1 ;
$t = "$n. this text. Array contains $cnt element(s). " ;
$arr[] = $t ;
$n++ ;
}
And that did wonders to understand what is going on.
You're on the right track :)
So, along this route I reworked my own code....
BUT in my code I still see only item 0 gets created and the count reveals
only this item?


Make a copy of your script, delete all unnecessary stuff from it
until you get a /minimal/ script that exhibits the error. Correct the
error in the minimal script then apply the same changes to your full
script.
You may even find why the error crept in while you strip the code :)

Alternatively, post your code here (after the stripping, please) and
someone will surely tell you where it is going wrong.
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #6
"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...

You're on the right track :)
So, along this route I reworked my own code....
BUT in my code I still see only item 0 gets created and the count reveals only this item?


Make a copy of your script, delete all unnecessary stuff from it
until you get a /minimal/ script that exhibits the error. Correct the
error in the minimal script then apply the same changes to your full
script.
You may even find why the error crept in while you strip the code :)

Alternatively, post your code here (after the stripping, please) and
someone will surely tell you where it is going wrong.


Thanks Pedro!

On the right track. But the track is longer than this problem. Well I don't
mind as long as I keep learning. The goal of traveling is doing just that.

My original question contained more (problems). And I think I see the origin
of the error already.
If I need help again I will post it here.

Thanks again.

pablo
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: lou zion | last post by:
hi all, i've got a class that takes a parameterless function pointer as a parameter. i want to store that function pointer in a variable and i'm trying to figure out the syntax. i came up with...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
2
by: pargat.singh | last post by:
Hi Everyone: I am using ArrayList to store my objects. Currently i am using session to store this arraylist and when i open showModalDialog i reterive this value from session and assign to...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.