Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTszyan Lam
371 PointsGetting Confusion of Using Variables and Conditionals
I am getting confuse of using variables and conditionals. For example, what is the different between using "=" and "=="?
1 Answer
geoffrey
28,736 PointsA variable could be compared to a box where you stock something. And to put something in the variable you need to use the equal sign. It's not at all a comparison. So as exemple $AmountOfMoney = 2000; means I've 'justed assigned/stored 2000 to my variable $AmountOfMoney.
But as the equal sign is used to assign a value to a variable, how can you compare two variables ? Obviously not using an equal sign as It's already used to store information. So you to use two equal sign such as '=='
Another little exemple:
<?php
$cars = "four wheels";
$motorcycles = "two wheels"; // as you can see I'm assigning values to some variables, at this stage.
// And $cars has the value four wheels at the moment, $motorcycles, two wheels.
$cars = $motorcyles // I'm not doing a comparison here, I'm putting the value of $motorcycles into $cars, which means $cars isn't equal to "four wheels" anymore but has the value of "two wheels".
$cars == $motorcyles //I'm not assigning anything, I'm comparing, and $cars == $motorcycles is now TRUE, as they both have the same value which is "two wheels".
?>
It's just something you have to remember... "=" doesn't mean equal to when you code but more something like, "contains" while "==" means really equal to.