note 74364 rejected from language.operators by danbrown
| From: | danbrown@php.net | Date: | Fri, 30 Jan 2009 20:41:12 +0000 |
| Subject: | note 74364 rejected from language.operators by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149999@lists.php.net to get a copy of this message | ||
Note Submitter: mathiasrav at gmail dot com
----
<?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?";
?>