2

It's hard to ask search this question and the answer would help clean up my code a bit. Basically, in PHP, if I use:

if (this = that)

what is going on? Is it checking that the variable got assigned correctly or is it checking the truth of "that" and as an aside also assigning it to "this?"

I ask because it would be easier if the answer were the former due to it taking an extra line to assign it, then run the conditional, and then use the return of it later. I know it's miniscule to be harping over one line, but over an entire script it can add up. Thanks for any help.

TO CLARIFY: I want to essentially write the equivalent of the following:

$this = something;
if ($this)
  do things with $this

by writing it as

if ($this = $myFunctionCall)
  do things with $this;

all assuming that $this isn't set beforehand.

15
  • You could test it :) You'd remember, this way.
    – Lazlo
    Apr 28, 2011 at 3:46
  • also it should be: if (this == that)
    – Jamie
    Apr 28, 2011 at 3:48
  • Yeah, I really need to setup a XAMMP or something because it's such a pain to test these things when you have to upload to your server and such.
    – linus72982
    Apr 28, 2011 at 3:48
  • @Jamie, he's specifically asking about assignment in an if statement, so i think he meant what he has. Apr 28, 2011 at 3:48
  • 1
    @xanadont I agree, while you can see what's going, 99.9% of the time that you see if(this = that) it's a typo rather than intentional. IDEs actually highlight it as a probable error. So if another developer picks up your code, that will jump out as a mistake. At the very least you should leave a comment saying it's not a mistake, and by the time you've done that, you may as well do the 'standard' way of assigning in the line above the if.
    – Dan Blows
    Apr 28, 2011 at 22:24

10 Answers 10

7

It's assigning, and then it's checking the "trueness" of the value that was assigned.

1
  • Excellent, this is what I wanted to hear, thanks to all the answers below as well.
    – linus72982
    Apr 28, 2011 at 3:52
2

its checking the "truthiness" of this iirc, the assignment occurs before evaluation

1
  • @prodigitalson, one of my favorite non-words Apr 28, 2011 at 3:52
2

Linus,

Regardless of if it works, I'm not a fan of the assignment-within-test approach. I think it's too-easy to overlook the ASSIGNMENT, which is a potential (and easily avoidable) source of future bugs... I think this commonly used coding paradigm is "a bit sloppy".

In almost all "modern" languages you can create and assign a local variable in one step... which I find succinct, self explanatory, and emminently readable.

var stuff = getStuff();
if (stuff == null) {
  Message("Sorry, no stuff found.");
  return;
}

verses

var thing, widget, stuff, foo;

.... then many lines later ...
if ((stuff=getStuff())) {
  // do things with stuff
} else {
  // no stuff
  Message("Sorry, no stuff found.");
}

As you can tell, I'm also anything but a fan of the old "structured programming" adage that there should be one exit-point per function/method/script. Instead, I believe that whenever we strike a situtation that means we can't continue here, we should leave, by the most direct route possible. This is JUST a personal preference. You can make valid arguements for any approach.

One word of advise: Whatever you do, do it CONSISTENTLY! The programmer who (a few years down the track, when you've moved on) is charged with enhancing your code will catch-on quick enough to "your style". They may not like it, but that's really not important, so long as they UNDERSTAND it. There's always a trade-off between succinctness and explicitness... there is no "correct" style... just some styles seem to WORK better than others, in the long run.

KISS it my son, and booger to brevity.

Cheers. Keith.

1

It will first set $this to $that, and then if $this == true, the contents of the if statement will be executed.

1

You would be assigning the value of that to this and checking if this then evaluates to true.

1

Yes, it first assigns and then checks if it's true.

Like when traversing MySQL tables:

while ($row = mysql_get_row($query)) {
  ...
}

When it doesn't work anymore (a false pops up), the loop breaks. It assigns and checks validity, all in one line.

1

I want to say that it assigns as well as check the value, purely based on this code

while($row = mysql_fetch_assoc($query))
{
    // Query actions
}

Which we all know loops until there are no more results to process. mysql_fetch_assoc returns false when no results remain, but the value is still assigned, hence, it doesn't check that the value was assigned, but the value that was assigned.

1

An assignment operation ($foo = 'bar') is an expression in which a value is assigned to a variable. The expression as a whole returns a value. The returned value is the value that was assigned.

So, if ($foo = 'bar') is synonymous to:

$foo = 'bar';
if ('bar') ...
0

Some demo code

$false = false;
$true = true;
$someVar = true;

function someFunk() {
  return false;
}

if ($someVar = $false) {
  echo "True \n";
} else {
  echo "False \n";
}

if ($someVar = $true) {
  echo "True \n";
} else {
  echo "False \n";
}


if ($someVar = someFunk()) {
  echo "True \n";
} else {
  echo "False \n";
}

This outputs

False 
True 
False

http://codepad.org/8JxX8MYP

0

if (this = that)

step 1->
this=that; //copy data from that to this

step 2->
If copy succeed then the code became like this

if (1) //true

If copy failed then the code became like this

if (0) //false

2
  • How would an assignment fail?
    – deceze
    Apr 28, 2011 at 4:04
  • $str='hi'; $str=print_r($array);
    – Sourav
    Apr 28, 2011 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.