CodeIgniter Forums
PHP boolean value assignment - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: PHP boolean value assignment (/showthread.php?tid=36679)



PHP boolean value assignment - El Forum - 12-10-2010

[eluser]Madmartigan1[/eluser]
I see a lot of people doing this:

Code:
if ($foo === $bar)
{
     $baz = true;
}
else
{
     $baz = false;
}

or...

Code:
$baz = $foo === $bar ? : true : false;

Am I missing something?
Isn't this exactly the same, in every case, as this?
Code:
$baz = $foo === $bar;



PHP boolean value assignment - El Forum - 12-10-2010

[eluser]WanWizard[/eluser]
Yes, it is.

Try only difference is the readability, which reduces are you go down the list of your examples...


PHP boolean value assignment - El Forum - 12-10-2010

[eluser]Ochetski[/eluser]
Its not common to people use a comparison as value to a variable, that's the reason.


PHP boolean value assignment - El Forum - 12-11-2010

[eluser]pkrstic[/eluser]
Most of them don't know for last solution. Personaly I use it a lot, for simple expressions this is easier to read than first and second solution.

Also this can be used for any kind of comparsion when you need boolean as result.