note 114555 deleted from control-structures.alternative-syntax by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:35:23 +0000 |
| Subject: | note 114555 deleted from control-structures.alternative-syntax by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215210@lists.php.net to get a copy of this message | ||
Note Submitter: josh at qaribou dot com
----
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;
?>