Re: Assignment in Conditional - How are they evaluated?

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

 



On Thu, Oct 29, 2009 at 10:23 AM, Martin Scotta <martinscotta@xxxxxxxxx> wrote:
> On Thu, Oct 29, 2009 at 11:11 AM, Robert Cummings <robert@xxxxxxxxxxxxx>wrote:
>
>> Ashley Sheridan wrote:
>>
>>> On Thu, 2009-10-29 at 13:58 +0000, Mark Skilbeck wrote:
>>>
>>>  How is the following evaluated:
>>>>
>>>> [code]
>>>> if ($data = somefunc()) ...
>>>> [/code]
>>>>
>>>> Ignoring the 'assignment inside condition' arguments, is the return value
>>>> of somefunc() assigned to $data, and then $data's value is evaluated (to
>>>> true or false), or is the actual assignment tested (does the assignment
>>>> fail, etc)?
>>>>
>>>>
>>>
>>> I believe that it determines if the return value of somefunc() is
>>> non-false. It will have the added benefit then that you can use the
>>> return value afterwards if it was, for example, not true, but a string
>>> or something instead.
>>>
>>
>> I do this all the time... an example is the following:
>>
>> <?php
>>
>> if( ($user = get_current_user()) )
>> {
>>    // Yay, we have a user... do something.
>>    echo $user->name();
>> }
>> else
>> {
>>    // Handle no current user.
>>    echo 'Anonymous';
>> }
>>
>> ?>
>>
>> Cheers,
>> Rob.
>> --
>> http://www.interjinn.com
>> Application and Templating Framework for PHP
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
> There is a situation when common-sense can fail...
>
> if( $a && $b = do_something() )
>
> The problem here is the precedence between && and =
> The correct sentence will be...
>
> if( $a && ($b = do_something()) )
>
> C coders knows this behaviour very well.
>
> cheers,
>  Martin Scotta
>
>
>
> --
> Martin Scotta
>

Assignment operations in PHP have the "side effect" of returning the
assignment.  For example:

function return_false() {
  return false;
}

var_dump(return_false()); //bool(false);
var_dump($a = return_false()); //bool(false);
var_dump($a = 1); // int(1)
var_dump($a = "hello world!"); //string...

So the same thing that allows you to do:

$a = $b = $c = $d = 154;

which works because "$d = 154" returns 154, which is assigned to $c,
which returns 154... is how assignment in conditionals or looping
works:
if($a = return_false()) { }
var_dump($a); //bool(false)

if($a = "hello") {}
var_dump($a); //string, "hello"

So what's really happening is the return value of the expression "$a =
____" is evaluated and that's used to determine the truth of the
conditionality.  if($a = return_false()) is exactly the same thing as
if(return_false()) save for you "capture" the output of the function,
rather than just allow the conditional operator to see it.  It's
functionally equivalent to $a = return_false(); if($a) {} but it's
important to understand that __assigning a variable to a value in PHP
is an expression with a return value___ and that return value is the
value that you assigned to the variable.

-- 
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