note 57998 added to language.expressions

From: Date: Fri, 21 Oct 2005 01:28:14 +0000
Subject: note 57998 added to language.expressions
Groups: php.notes 
Request: Send a blank email to php-notes+get-97334@lists.php.net to get a copy of this message
At 04-Feb-2005 05:13, tom at darlingpet dot com said: > It's also a good idea to use parenthesis when using something SIMILAR to: > > <?php > echo (trim($var)=="") ? "empty" : "not empty"; > ?> No, it's a BAD idea. All the short-circuiting operators, including the ternary conditional operator, have LOWER precedence than the comparison operators, so they almost NEVER need parentheses around their subexpressions. Inserting the parentheses suggested above does not change the meaning of the code, but their use misleads inexperienced programmers to expect that things like this will work in a similar manner: <?php function my_print($a) { print($a); } my_print (trim($var)=="") ? "empty" : "not empty"; ?> when of course it doesn't. Rather than worrying that code doesn't work as expected, simply learn the precedence rules (http://www.php.net/manual/en/language.operators.php) so that one expects the right things. ---- Manual Page -- http://www.php.net/manual/en/language.expressions.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57998 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57998&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57998&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57998&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57998&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57998&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57998&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57998&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57998&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97334) next »