By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

Function Parameter passing? Session? Static? Global?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.