note 97559 added to function.print
| From: | peev dot alexander at gmail dot com | Date: | Mon, 26 Apr 2010 09:16:03 +0000 |
| Subject: | note 97559 added to function.print | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168638@lists.php.net to get a copy of this message | ||
(As a response to
user at example dot net
25-Aug-2008 04:47)
There is an easier way (than bracket wrapping) to have all print statements in an if condition
executed while keeping them separated:
<?php
if (print("foo") and print("bar") and print("baz")) {
// "foo" and "bar" and "baz" have been printed
}
?>
or
<?php
if (print("foo") xor print("bar") xor print("baz")) {
// "foo" and "bar" and "baz" have been printed
}
?>
This is true, because of the low precedence of the 'or', 'and', and
'xor' operators.
If you use the 'or' operator instead of 'and' or 'xor', only
"foo" will be printed, because print always returns int(1) (which obviously evaluates to
true), and this will stop further evaluation.
Of course if you use the 'xor' operator and have an even amount of if statements, the
if-block will not execute.
You could also use it like this:
<?php
$print_remains = false;
if (print("foo") and $print_remains and print("bar") and
print("baz")) {
// This if-block will not execute
}
?>
This will only print "foo", and will not execute the if-block.
Hope this helps someone.
----
Server IP: 69.147.83.197
Probable Submitter: 87.120.68.77
----
Manual Page -- http://www.php.net/manual/en/function.print.php
Edit -- https://master.php.net/note/edit/97559
Del: integrated -- https://master.php.net/note/delete/97559/integrated
Del: useless -- https://master.php.net/note/delete/97559/useless
Del: bad code -- https://master.php.net/note/delete/97559/bad+code
Del: spam -- https://master.php.net/note/delete/97559/spam
Del: non-english -- https://master.php.net/note/delete/97559/non-english
Del: in docs -- https://master.php.net/note/delete/97559/in+docs
Del: other reasons-- https://master.php.net/note/delete/97559
Reject -- https://master.php.net/note/reject/97559
Search -- https://master.php.net/manage/user-notes.php