AlexBeau
AlexBeau

Reputation: 103

Question Regarding Variable State in IF Statement

I am a dead beginner with PHP and have been reading through 'PHP for the Web: Visual Quickstart Guide 4th Ed.' by Larry Ullman and have a question regarding something I came across in the book.

At the end of each chapter he has a few questions for review and I am stuck on one of them and not sure if I have the correct answer or the correct train of though regarding it.

The question is as follows:

Without knowing anything about $var will the following conditional be TRUE or FALSE? Why?

if ($var = 'donut') {...

I am apt to say that it will be false because we don't know if $var has been assigned the value donut yet within the program but I am not sure.

Can anyone help explain this to me so I can grasp this concept and feel confident about it?

Upvotes: 3

Views: 138

Answers (5)

Michael Berkowski
Michael Berkowski

Reputation: 270727

This conditional will always evaluate to TRUE because the value donut is assigned, and then the value of $var is returned to the if() statement. The assignment happens first.

A successful assignment to a variable causes that variable to be returned immediately. A non-empty string is a "truthy" value, and is returned as such.

If instead it was assigned as:

if ($var = "") {}

It would evaluate to FALSE, according to PHP's boolean evaluation rules:

var_dump((bool) "");        // bool(false)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)

Addendum

Just to add, as a practical example of assignment inside a flow control conditional you probably see almost every day -- the while() loop we typically use to retrieve a rowset from a MySQL result resource:

while ($row = mysql_fetch_assoc($result)) {
  // executes this inner block as long as $row doesn't 
  // recieve a FALSE assignment from mysql_fetch_assoc()
  // reaching the end of its rowset
}

Upvotes: 1

iLLin
iLLin

Reputation: 760

To make things simpler here is a better explanation.

<?php
// To assign a value to a variable you do this
$var = 'donut';

// To evalute the value of a variable you do this.
if($var == 'donut') { }
// Notice the existence of double equals here.

// If you have code like this:
$var = 'donut holes';
if ($var = 'donut') {
  // This is true because any value you assign with ONE equals is always TRUE
  print $var; // Will output 'donut' because you reassigned it.
}

Upvotes: 1

Abdullah Shoaib
Abdullah Shoaib

Reputation: 2095

it will be true as $var = 'donut' is an assignment and not 'is equals to (==)'. The = operator assigns the value of the right hand side to the variable on the left hand side.The == operator checks whether the right hand side is equal to the left hand side.

Upvotes: 1

Warface
Warface

Reputation: 5119

It will be true since the $var variable is define to be 'donut', if the $var variable is empty then it should be returning false.

Example

$var = ''; // False
$var = 'something something'; //True

Upvotes: 2

Chris McClellan
Chris McClellan

Reputation: 1105

There is only one equal sign so it will return true. Heres why: It is assigning "donut" to $var which makes $var true. :)

If the statement had 2 or 3 equal signs we wouldn't know what it would return.

Upvotes: 2

Related Questions