开发者

If (this = that) - what actually happens here?

开发者 https://www.devze.com 2023-03-01 00:33 出处:网络
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:

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 con开发者_运维技巧ditional, 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.


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


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


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.


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


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


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.


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.


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') ...


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消