Actually, I disagree with the explenation here. The reasoning behind it
is correct, but the explenation contains a number of incorrect statements.
The = (a single '=') is the so-called assignment operator. This operator
assigns the right-hand value to the left-hand variable. So, when you
write $a = 5; you'll assign the right-hand value (5) to the left-hand
variable ($a).
The == (a double '=') is the comparation operator. This operator
compares the right-hand value to the left-hand value, and returns the
boolean true if it is the same, or false if it is not.
The === (a triple '=') is also a comparation operator, however this one
is more strict. This means (in most cases) that where '==' doesn't care
about if the left-hand value and right-hand value are of different types
(eg. integer vs. string), this one does. So, you could say that when ===
returns true, == would also return true. However, when === returns
false, == doesn't necesserily need to return false aswell.
Now, the assignment operator, as each operator, has a return value. And
the return value of the assignment operator is what probably confused
you. Basically, what happens is that the assignment operator "returns"
the value it just assigned. So, if you do $a = 5; the return value will
be 5. This is why you can do $a = $b = 5. This will be interpreted as
being $a = ($b = 5); which means: $b = 5 returns 5, which is then
assigned to $a, which makes $a have the value 5.
In most cases, the return value of the thing is ignored. eg. if you do
$a = 5; 5 will be assigned to $a, and the result (5) will be "returned".
But since we don't do anything with the return value, it's silently ignored.
So, how do if() and such constructs work with this, you wonder?
if(), and most other language constructs (while(), elseif(), etc) take
an expression as its argument, compare it to true (lazy comparing), and
will continue if it returns true, or stop/skip if it's false.
if() could also be written as if(expression == true), where expression
is anything with a return value (including operators!).
So, if you write if(2 == 2), the return value will be true, which equals
true, and thus the if() will be executed. However, if you have if(3 ==
2) it'll return false (2 is NOT the same as 3), which is obviously not
true, and thus the if() will be skipped.
When we said before, assignment operators return the value they just
assigned, you'll see that: if($a = true) will return true, which is
equal to true, and which will cause the if() to be executed. On the
other side, doing if($a = false) will return false, which is not equal
to true, and which will make PHP skip the if().
Because php uses "lazy" comparing in the if(), it will not check the
type of the variable/value! This means that for the if, this is true:
if(2 == '2'). And also if($a = 2) (all values are true, except NULL,
false, an empty string, or zero. There is a full table showing these in
the manual). So, doing if($a = 0) will return false, and will skip the
if(). Of course, doing if('2' === 2) will also return false (due to the
use of ===) and will skip the if aswell.
Hope that clears up any misunderstandings that might have been left.
Also, I urgently recommend you to have a look at the wonderful PHP
manual (one of THE best programming/scripting language manuals, if I may
be so bold). A few pages which will surely help you (and also explain
the above):
http://www.php.net/manual/en/language.operators.assignment.php
http://www.php.net/manual/en/language.operators.comparison.php
http://www.php.net/manual/en/types.comparisons.php
http://www.php.net/manual/en/language.control-structures.php
etc.
Please read the manual first, it will answer a lot of your questions.
- tul
Łukasz Hejnak wrote:
Erik Johnson napisał(a):
Okay.. I was just wondering; What's the difference between = and ==?
Would
it matter if I changed them? It seems as though if I put = instead of
the
=='s, it comes up ONLY page two no matter what $page equals, and if
it's ='s
instead of =='s, then it comes up with only page one no matter what $page
equals.
It's a *BIG* difference!
Because single '=' means that whatever stands at the right side of the
'=' should be assigned to whatever stands at the left side,
ie.
$a=5
doesn't compare if a is equal 5, only assignes the variable 'a' it's
value which will be 5 after this code
so if You write
if ($a=5) {...}
it will always be executed (therefore else isn't executed), as
assignment always returns true :]
so when using 'if' You always need to do
if ($a==5) {...}
unless of course Youd want to do an assingment and some code, but then
why not do
$a=5;
...
? :)
--
Best wishes
Łukasz 'Szift' Hejnak
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php