> -----Original Message-----
> From: Chris Knipe [mailto:[EMAIL PROTECTED]]
> Sent: 10 May 2002 04:38
>
> Just to clarify myself above...
> if ($var1 = $var2) { .....
>
> In my crazy head, I see $var1 and $var2 to be in a sort of
> "read-only" state
> inside the () of the if statement. For a if statement to be
> able to change
> the values in that instance, would be wrong for me.
> Afterall, the logic of
> a if statement does go something in the lines of "if value
> equals value then
> execute this, else, execute that" etc etc etc.... There's
> nothing in that
> logic of setting or changing any values.... ?
That's not really correct. Strictly speaking, what's inside the () of an if (or
while, or switch, ...) is an expression -- and in PHP (as in many C-like languages)
an assignment is actually an expression the result of which is the value assigned.
This is why constructs such as
a = b = c = 0
work: the assignment expression "c = 0" is first evaluated, giving the result 0; this
result is then assigned to b, also giving the result 0, which is then assigned to a.
But back to the if problem; consider:
if (func($y)):
// lots of code using func($y) several times
endif;
Well, if func($y) is some big function that is costly to execute, you really don't
want to be re-evaluating it each time you want the result, so you might think of
recoding this as:
$x = func($y);
if ($x):
// lots of code using $x several times
endif;
which only evaluates func($y) once. But using the assignment-as-expression technique,
you can shorthand this as:
if ($x = func($y)):
// lots of code using $x several times
endif;
Now, this might no seem like much of an advantage, but look at this while loop:
while ($x = func($y)):
// lots of code using $x several times
endwhile;
Without the assignment-as-expression in the while (), you'd have to code this as:
$x = func($y);
while ($x):
// lots of code using $x several times
$x = func($y);
endwhile;
Not only are you having to duplicate the assignment, the duplicates may be several
hundred lines of code apart; the shorthand is clearer, less error-prone, and probably
fractionally more efficient to boot!
Of course, this can also lead to strange-looking constructs such as:
if ( ($x = func($y)) == 1):
which, when you break it down, actually makes perfect sense (exercise for the reader!)
Finally, a quick reference for =, ==, and ===:
= assignment operator
== equality test (values must be equivalent)
=== "strict equality" test (values AND TYPES must match)
NOTE: the == equality test checks for equivalent values, using automatic type
conversion if necessary; this can sometimes lead to unexpected results, since 0 ==
"abc" and "0" == FALSE. Use the === operator if you want to be sure that two values
are absolutely identical.
Hope this little lot helps a bit!
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php