-1

I use PHP around 3 months see many syntax.
but cannot understand what is this mean.

if($foo = 'somevalue') //or if($foo = $somevalue)
{

}

I see only if($foo) this mean if($foo == TRUE). or if($foo ==,>,< somevalue)
but in this case, if$foo hold some value???

doflamingo
  • 567
  • 5
  • 23

4 Answers4

2

You are just simultaneously assigning a value to $foo and evaluating it in the if statement. So if the value of $foo corresponds to false, then code inside the if statement would not execute. As an example:

if ($foo = 12) { // foo's value is now 12
    // code here would execute
}
echo $foo; // 12

if ($foo = 0) { // foo's value is now 0
    // would not execute
}
echo $foo; // 0
Rax Weber
  • 3,730
  • 19
  • 30
2

as many will say = assigns the value. however this is useful in if statements aswell

the if statement if($foo = 'somevalue') is generally bad because it will always evaluate to true. however if($foo = $somevalue) is somewhat a bit better because it's similar to what you see when checking the results of a database query, for instance with

if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) 

if $mysqli->query is successful in running the if statement evaluates to true because the succesful retunr result of the function is not considered FALSE and inside your if statement you can expect that $result to be usable for other mysqli_* function.

however if $mysqli->query is not successful it'll return false and the if statement evaluates to false and you can set up an else statement to output an error. you will get the same result if you did

$somevalue = $mysqli->query("SELECT Name FROM City LIMIT 10")
if($foo = $somevalue)

but you kinda waste time doing an extra line like this when you can just skip $somevalue all together

Memor-X
  • 2,870
  • 6
  • 33
  • 57
0

This if statement will simply either

  • return true and assign the string somevalue to $foo
  • return false because somevalue cannot be assigned to $foo

In a lot of cases, this is a mistake in the writing of that if statment, where someone might want to check if the value of $foo is the string somevalue.

To properly evaluate that, one would want the following

if ($foo == 'somevalue') //$somevalue

Where = would assign a new value, == compares two values. see this question for more information on that.

Community
  • 1
  • 1
Luke
  • 640
  • 3
  • 10
0

When you have a single = sign, it signifies assignment operator. i.e The if condition will always return true.

$a = 2;
if ($a = 3) {
    echo "True";
} else {
    echo "False";
}

//Output: True

Whereas, if you use logical operator ==, it will compare the two values and return boolean true/false.

$a = 2;
if ($a == 3) {
    echo "True";
} else {
    echo "False";
}

//Output: False
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
  • `The if condition will always return true.` is not true. https://eval.in/657910 – chris85 Oct 10 '16 at 03:36
  • Well, if you `var_dump($a=3);` here, it will clearly return `int(3) True` which satisfies the if condition. – Indrasis Datta Oct 10 '16 at 03:38
  • Yes, if it is `3`. If it is false (as demo showed) though it will not `always` be `true`. For example look at the manual, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php, if the assignment were always true `if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) {` wouldn't make sense. – chris85 Oct 10 '16 at 03:42