...

View Full Version : plus sign in IF statement



crmpicco
01-27-2010, 01:04 PM
Hi,

Does anyone know what the "+" is used for in this IF statement? I have found it in a script, but have no idea what it is for.



if (+$default_stadium_id) {
// ... code in here ...
}


Thanks,
Picco

Pcfr43k
01-27-2010, 01:16 PM
Its an operator.
I don't know enough of PHP to know what it does here, but maybe this helps:
http://www.tizag.com/phpT/operators.php

Preincrement, when something is executed before

Fou-Lu
01-27-2010, 01:19 PM
It does nothing. It will not increment a variable, flip a negative bit, or assign the variable to another or a new value. It also will not throw an error.

ssonawa
01-27-2010, 01:48 PM
<?
$a = -1;
$b = 1;

if (+$a>0 ){
echo "Print it!";
}
?>
Output of above code is nothing.

It does not do anything nor flips negative value to positive.Why dont you try to change values of the variable here? And look for whats expected there.

Check for operator overloading if its a class member.

Fou-Lu
01-27-2010, 03:27 PM
PHP doesn't support operator overloading with the exception of index control using the ArrayAccess interface.

Oh, I should mention though that php does support one unary: -$var. That will flip it. Never used it though, so I have no idea what it actually does. The documentation is ambiguous as to if the 'opposite' is just flipping the sign bit, or if the 'opposite' is flipping all bits, and I can't test this atm. I'd suspect just the sign bit, since we have the bit not (~).

crmpicco
06-24-2010, 11:08 PM
Strange one I guess, i'll have a look into "bit flipping" as I guess that is something that could raise it's head in a Zend Exam.

But I presume from the comments here that the "+" in the if condition was simply to prevent the code from ever running, without actually removing it from the script or commenting it out.

Cheers,
Picco

Fou-Lu
06-24-2010, 11:36 PM
No, it will still execute normally. The + literally does nothing, it doesn't empty a variable or alter it in any way.
If you run this:


$t = -1;
if (+$t < 0)
{
print $t;
}

This will still execute and print -1. On the other hand, the - does have an effect:


$t = 1;
if (-$t < 0)
{
// Directly flipped the sign:
print $t;
}

This will execute and print 1.
Also:


$t = -1;
if (-$t < 0)
{
print $t;
}

Will not execute. It evaluates the negative value of $t which is negative already, and therefore can not be less than 0. Therefore, this will not print.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum