note 114555 added to control-structures.alternative-syntax

From: Date: Wed, 05 Mar 2014 22:17:06 +0000
Subject: note 114555 added to control-structures.alternative-syntax
Groups: php.notes 
Request: Send a blank email to php-notes+get-198807@lists.php.net to get a copy of this message
The reason temec987's approach of using boolean operators as an alternative to control structures won't work for an 'echo' is because the result of evaluating the expression will always be a boolean. Other languages (e.g. ruby) are much better suited to this approach, as the expression evaluated will be the resultant value, e.g.: 5 && 4 In ruby, this would be 4, but in PHP, this would be true (type-juggled equivalent is 1), which isn't useful for anything but further binary logic. You can still use logical operators as conditionals, but only for executing logic, not for getting a value back, e.g.: <?php defined('USER_CAN_EXECUTE') or die('Access denied.'); ?> is a nice one to use for access control, or say you want to put in a quick check that your object has all the data loaded it needs to call a webservice (functions are just examples): <?php $this->readyForService() and $this->postData('http://endpoint.com'); ?> What you can't use them for is something like this: <?php echo (strlen($mystring) > 5) and $mystring; ?> instead, you'd use ternaries for that: <?php echo (strlen($mystring) > 5) ? $mystring : null; ?> ---- Server IP: 64.68.200.35 Probable Submitter: 96.45.207.227 ---- Manual Page -- http://php.net/manual/en/control-structures.alternative-syntax.php Edit -- https://master.php.net/note/edit/114555 Del: integrated -- https://master.php.net/note/delete/114555/integrated Del: useless -- https://master.php.net/note/delete/114555/useless Del: bad code -- https://master.php.net/note/delete/114555/bad+code Del: spam -- https://master.php.net/note/delete/114555/spam Del: non-english -- https://master.php.net/note/delete/114555/non-english Del: in docs -- https://master.php.net/note/delete/114555/in+docs Del: other reasons-- https://master.php.net/note/delete/114555 Reject -- https://master.php.net/note/reject/114555 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198807) next »