note 46666 modified in language.operators.comparison by danbrown

From: Date: Thu, 18 Jun 2009 14:15:46 +0000
Subject: note 46666 modified in language.operators.comparison by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-157089@lists.php.net to get a copy of this message
if you want to use the ?: operator, you should be careful with the precedence. Here's an example of the priority of operators: <?php echo 'Hello, ' . isset($i) ? 'my friend: ' . $username . ', how are you doing?' : 'my guest, ' . $guestusername . ', please register'; ?> This make "'Hello, ' . isset($i)" the sentence to evaluate. So, if you think to mix more sentences with the ?: operator, please use always parentheses to force the proper evaluation of the sentence. <?php echo 'Hello, ' . (isset($i) ? 'my friend: ' . $username . ', how are you doing?' : 'my guest, ' . $guestusername . ', please register'); ?> for general rule, if you mix ?: with other sentences, always close it with parentheses. --was-- if you want to use the ?: operator, you should be careful with the precedence. Here's an example of the priority of operators: echo 'Hello, ' . isset($i) ? 'my friend: ' . $username . ', how are you doing?' : 'my guest, ' . $guestusername . ', please register'; This make "'Hello, ' . isset($i)" the sentence to evaluate. So, if you think to mix more sentences with the ?: operator, please use always parentheses to force the proper evaluation of the sentence. echo 'Hello, ' . (isset($i) ? 'my friend: ' . $username . ', how are you doing?' : 'my guest, ' . $guestusername . ', please register'); for general rule, if you mix ?: with other sentences, always close it with parentheses. http://php.net/manual/en/language.operators.comparison.php

« previous php.notes (#157089) next »