Re: Assignment in Conditional - How are they evaluated?

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

 



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


[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