[PHP] Whats the difference?

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

What is the difference between these?

Code:
$str = 0;

and

Code:
$str == 0;

...and where would i use them and why?

Thanks.
 
Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
= is the assignment operator. It's used to assign a value to a variable, for example:
Code:
$bar = 1337;
$foo = $bar;
It returns the value that was assigned to the variable on the left, so the following code has the same effect:
Code:
$foo = ($bar = 1337);

== is the comparison operator. It's used to compare two values, and returns true if they are equal, false otherwise, for example:
Code:
$boolean = $foo == $bar; // $boolean is set to true if $foo and $bar are equal

if ($foo == $bar)
{
    // do something
}
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
Inquisitor's second point looks useless, but is actually really useful when you want to initialise a load of variables to the same value:

Code:
$foo = $bar = $baz = $mung = null;
 
Back
Top Bottom