note 74364 added to language.operators
| From: | mathiasrav at gmail dot com | Date: | Sat, 07 Apr 2007 16:41:12 +0000 |
| Subject: | note 74364 added to language.operators | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125175@lists.php.net to get a copy of this message | ||
<?php
"This might be related to the comment 31-Oct-2006 07:20 just below, but here goes.";
"I have found a bug (might be inherited from the way C does it) in PHP's operator
precedence. Things like:";
if ($a != ($a = $b)) {/* more things */}
"doesn't work. In my code, I've changed that to:";
if ($a != $b) {$a = $b; /* more things */}
"A longer explanation (and a practical example) follows.";
"I've written an IRC bot which (among other things) can parse an IRC string and find out
how the individual characters are formatted.";
// Quick guide to IRC formatting: In IRC, there are several 'format control characters',
all non-printable. Among these are the bold formatting character, 0x02, which I'll write as
<b>, and the underline formatting, 0x1F, which I'll write as <u>.";
"My code parses a string and reads these characters and makes an array which contains the
individual characters and their formatting.";
"Here's an example. (I named the indices of the array, my code actually uses numbered
indices)";
$ourstring = array(
array(
'Hello ',
'bold' => 1,
'underline' => 0,
), array(
'world.',
'bold' => 0,
'underline' => 1,
)
);
"And now, my formatparsedarray-to-ircstring function, that translates this to a string which
can be posted to an IRC channel.";
$resstring = '';
define('bold', "\x02");
define('underline', "\x1F");
$bold = false;
$underline = false;
foreach ($ourstring as $c) {
if ($bold != ($bold = $c['bold'])) $resstring .= bold;
if ($underline != ($underline = $c['underline'])) $resstring .= underline;
$resstring .= $c[0];
}
"Now, $resstring should contain:";
$resstring == "\x02Hello \x02\x1Fworld.";
"However, it doesn't, since ($bold = $c\['bold'\]) is evaluated before ($bold ==
(...)). According to the operator precedence, == is evaluated left-to-right, and = is evaluated
right-to-left, but in this case, == evaluated right-to-left.";
"Is this a bug, or is it a leftover from C's way of parsing such things?";
?>
----
Server IP: 86.58.145.50
Probable Submitter: 213.237.66.155
----
Manual Page -- http://www.php.net/manual/en/language.operators.php
Edit -- https://master.php.net/note/edit/74364
Del: integrated -- https://master.php.net/note/delete/74364/integrated
Del: useless -- https://master.php.net/note/delete/74364/useless
Del: bad code -- https://master.php.net/note/delete/74364/bad+code
Del: spam -- https://master.php.net/note/delete/74364/spam
Del: non-english -- https://master.php.net/note/delete/74364/non-english
Del: in docs -- https://master.php.net/note/delete/74364/in+docs
Del: other reasons-- https://master.php.net/note/delete/74364
Reject -- https://master.php.net/note/reject/74364
Search -- https://master.php.net/manage/user-notes.php