Reputation: 10117
Im using an if statement to determine what to return in a function, but it seems to be not working the way i want it to.
function DoThis($dogs, $cats){
// do something with dogs, pet them perhaps.
$reg = $dogs[0];
$nate = $dogs[1];
if($cats = "dave"){return $reg;}
if($cats = "tom"){return $nate;}
}
$cats
is a string (if that helps), and when entered it doesn't yield any return.
If i manually set a return, that works, but the above doesnt for some reason.
Upvotes: 2
Views: 138
Reputation: 134157
To test for equality, use the == (double equals) operator instead of the = (single equals) operator.
For example:
if("dave" == $cats){return $reg;}
if("tom" == $cats){return $nate;}
Upvotes: 6
Reputation: 57902
You need to use == to compare.
= is an assignment, so it has the effect of setting $cats to "dave" and then (because the expression evaluates to "dave", which is non-empty) it treats the if statement as being "if (true) ..." and executes the contained code.
Upvotes: 2
Reputation: 166066
You're using the assignment operator instead of the comparison operator. Try the following instead.
$cats == "dave"
$cats == "tom"
When you say
if($cats = "dave") { ... }
you're really saying
It's a common mistake, and something tha plagues old hands and new hands alike.
Upvotes: 6