CodeIgniter Forums
Just gotta love theory... Why aren't these two stmts doing the same thing? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Just gotta love theory... Why aren't these two stmts doing the same thing? (/thread-35264.html)



Just gotta love theory... Why aren't these two stmts doing the same thing? - El Forum - 10-24-2010

[eluser]stormbytes[/eluser]
What can I say? I love wrapping my head around concepts & the underline 'guts', rather then being all about monkey-see-monkey-copy/paste.

Below are two statements which, imho, should do exactly the same thing! Yet one works, and one does not. I'd love to hear from a CI-guru (even if this isn't a CI-question) what the practical difference is in how each is evaluated.

Statement # 1:
Code:
$prefix = isset($config['_prefix']) ? $config['_prefix'] : '';

Statement #2:
Code:
$prefix = isset($config['_prefix']) OR '';



Just gotta love theory... Why aren't these two stmts doing the same thing? - El Forum - 10-24-2010

[eluser]tonanbarbarian[/eluser]
actually the second statement should return a boolean result.
do
Code:
var_dump($prefix);
after the second statement and see if the result is boolean


Just gotta love theory... Why aren't these two stmts doing the same thing? - El Forum - 10-24-2010

[eluser]WanWizard[/eluser]
Indeed. The first one is an if-then-else shorthand, the second a boolean evaluation.


Just gotta love theory... Why aren't these two stmts doing the same thing? - El Forum - 10-24-2010

[eluser]stormbytes[/eluser]
[quote author="WanWizard" date="1287970697"]Indeed. The first one is an if-then-else shorthand, the second a boolean evaluation.[/quote]

So then what function, if any, is performed by the OR ? or is it disregarded?


Just gotta love theory... Why aren't these two stmts doing the same thing? - El Forum - 10-24-2010

[eluser]WanWizard[/eluser]
No, OR is a boolean operator.

So when PHP evaluates this line, it will convert both elements of the statement to boolean, if needed. Now, isset() already returns a boolean. '' is a string, and an empty string evaluates to FALSE (see this for an explanation).

Your statement therefore translates to:
Code:
$prefix = isset($config['_prefix']) OR FALSE;
which, given the fact that this is an OR, will ignore the FALSE all together, since it will be either 'TRUE OR FALSE' (equals TRUE), or 'FALSE OR FALSE' (equals FALSE).