Re: Tidying code for PHP5.0.5/PHP4.4.0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



thanks everyone for the crash course in better understanding the underlying mechanisms!
... I'm probably not the only one that learnt something from this ;-)

Dragan Stanojevic - Nevidljivi wrote:
Jochem Maas wrote:

Basically, in PHP, a reference (such as what key() takes as a parameter [1]) can only point to an actual variable, not directly to the result of a function. So you have to assign the output of the function to a variable first.

wtf, Im now officially confused (before I suffered unofficially :-)

if foo() expects one args by reference then why is doing:

foo($a = 5);

..wrong? I always thought that the expression (in this form) 'returns'
the variable? or does it need to be:?

foo( ($a = 5) );


Well think like this: expression != variable != value :]

Let me explain... most functions (and expressions!) return value, not variable (unless it's passed by reference). Usually, although most beginners don't know it, you ignore that value.

For example, you could do:

fopen( $filename, $filemode );

and in that case return value (not variable) of fopen is ignored. This is a bad practice because you need to check it, but this is just an example. Most other functions return values that we ignore.

Another example (not functions but expressions) is assigning a value:

$a = 5; // returns 5 (and we ignore it again)

it allows us to do the following:

$a = $b = $c = 10;

read from right to left, and it'll go something like this: "assign 10 to $c (it evaluates to 10), assign (previously assigned value) 10 to $b, and so on...".

So in essence: $c = 5 in expression and returns the value of 5 not a variable/reference which can be changed.

Hope this will help you a little,
N::

P.S. Just a thought... If you declare the method to return a ref maybe you can get away with it...

private public & getThis() {
    $tempArr = array( 'a', 'b', 'c' );

    return $tempArr;
}

as opposed to:

private public getThis() { ... }

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux