Assignment in Conditional - How are they evaluated?
Assignment in Conditional - How are they evaluated?
am 29.10.2009 14:58:30 von Mark Skilbeck
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)?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Assignment in Conditional - How are they evaluated?
am 29.10.2009 15:03:58 von Ashley Sheridan
--=-tH4mSr1Tii7ayk36tmHh
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
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.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-tH4mSr1Tii7ayk36tmHh--
Re: Assignment in Conditional - How are they evaluated?
am 29.10.2009 15:07:34 von Martin Scotta
--0015175d046ce037da0477136e1f
Content-Type: text/plain; charset=ISO-8859-1
On Thu, Oct 29, 2009 at 10:58 AM, 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)?
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
The code is interpreted this way...
$data = somefunc();
if ($data)
--
Martin Scotta
--0015175d046ce037da0477136e1f--
Re: Assignment in Conditional - How are they evaluated?
am 29.10.2009 15:11:36 von Robert Cummings
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:
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
Fwd: Assignment in Conditional - How are they evaluated?
am 29.10.2009 15:23:56 von Martin Scotta
--0015175d046c60d959047713a91f
Content-Type: text/plain; charset=ISO-8859-1
On Thu, Oct 29, 2009 at 11:11 AM, Robert Cummings 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:
>
>
>
> 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
--0015175d046c60d959047713a91f--
Re: Assignment in Conditional - How are they evaluated?
am 29.10.2009 15:40:32 von Eddie Drapkin
On Thu, Oct 29, 2009 at 10:23 AM, Martin Scotta wr=
ote:
> On Thu, Oct 29, 2009 at 11:11 AM, Robert Cummings w=
rote:
>
>> Ashley Sheridan wrote:
>>
>>> On Thu, 2009-10-29 at 13:58 +0000, Mark Skilbeck wrote:
>>>
>>> Â How is the following evaluated:
>>>>
>>>> [code]
>>>> if ($data =3D somefunc()) ...
>>>> [/code]
>>>>
>>>> Ignoring the 'assignment inside condition' arguments, is the return va=
lue
>>>> of somefunc() assigned to $data, and then $data's value is evaluated (=
to
>>>> true or false), or is the actual assignment tested (does the assignmen=
t
>>>> 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:
>>
>>
>>
>> if( ($user =3D 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 =3D do_something() )
>
> The problem here is the precedence between && and =3D
> The correct sentence will be...
>
> if( $a && ($b =3D 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 =3D return_false()); //bool(false);
var_dump($a =3D 1); // int(1)
var_dump($a =3D "hello world!"); //string...
So the same thing that allows you to do:
$a =3D $b =3D $c =3D $d =3D 154;
which works because "$d =3D 154" returns 154, which is assigned to $c,
which returns 154... is how assignment in conditionals or looping
works:
if($a =3D return_false()) { }
var_dump($a); //bool(false)
if($a =3D "hello") {}
var_dump($a); //string, "hello"
So what's really happening is the return value of the expression "$a =3D
____" is evaluated and that's used to determine the truth of the
conditionality. if($a =3D 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 =3D 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