Page 1 of 2

if ( $variable ) { } What does this mean?

Posted: Sat Jun 30, 2007 11:10 am
by Dave2000
Hi,

What's the difference between...

Code: Select all

if ( $variable == true ) { }
and

Code: Select all

if ( $variable ) { }
Sorry. I know this has been asked many times before, but I just have no idea what it is called/what terms to search for in order to find anything about it.

Thank you

Shears

Posted: Sat Jun 30, 2007 11:16 am
by volka
If $variable evaluates to true the condition is fulfilled.
see http://www.php.net/manual/en/language.t ... an.casting

Posted: Sat Jun 30, 2007 12:08 pm
by Dave2000
So ... if ( $variable == true ) ... and ... if ( $variable ) ... mean exactly the same thing. I didn't recall this being the case, but I guess the PHP manual doesn't lie :)

Thank you

Posted: Sat Jun 30, 2007 12:16 pm
by miro_igov
But note that

Code: Select all

if($variable) {}
is valid if the $variable is equal to 1,2 , "some text" , etc, while testing it against true is something different.

Posted: Sat Jun 30, 2007 12:26 pm
by volka
miro_igov wrote:But note that

Code: Select all

if($variable) {}
is valid if the $variable is equal to 1,2 , "some text" , etc, while testing it against true is something different.
What difference is there?

Posted: Sat Jun 30, 2007 12:34 pm
by miro_igov
if you want to check if($variable == 'test') or if($variable == '1024') the if($variable) always will be true. Just an idea when people should use if($variable) and if($variable == 'something'). Does make sense?

Posted: Sat Jun 30, 2007 12:36 pm
by Benjamin
You can run tests..

Code: Select all

$str_foo = 'hello';

if ($str_foo == true)
{
    echo "str_foo is equal to true";
}

if ($str_foo)
{
    echo "str_foo evaluates to true";
}


// will fail..
if ($str_foo === true)
{
    echo "str_foo is true";
}

$str_foo = true;

if ($str_foo === true)
{
    echo "str_foo is true";
}

Posted: Sat Jun 30, 2007 1:15 pm
by volka
miro_igov wrote:if you want to check if($variable == 'test') or if($variable == '1024') the if($variable) always will be true. Just an idea when people should use if($variable) and if($variable == 'something'). Does make sense?
Yes, but that wasn't the question ;)

Posted: Sat Jun 30, 2007 1:31 pm
by miro_igov
Of course it was not the question, it is supposed to be an answer. A better explanation.

I probably did not wanted to place a question on this subject , but an answer.

Re: if ( $variable ) { } What does this mean?

Posted: Sat Jun 30, 2007 1:47 pm
by volka
I meant the question was
Shears wrote:What's the difference between...

Code: Select all

if ( $variable == true ) { }
and

Code: Select all

if ( $variable ) { }
but not about $variable=='xyz' or $variable===true

Posted: Sat Jun 30, 2007 3:42 pm
by superdezign
Without using "===," if($variable) and if($variable == true) will always give the same result (as astions has kindly shown). That's the reason that we have "===."

Posted: Wed Jul 04, 2007 4:50 am
by afsin
I use this structure like that

Code: Select all

$link=$_GET["link"];
$folder=$_GET["folder"];
if (!$link || !$folder)
	require ('./firstpage.php');
	else
	{
	$page="./$folder/$link".".php";
	require $page;
	}
This also works properly. Is there any difference between using this and isset().

Posted: Wed Jul 04, 2007 5:02 am
by volka
If no parameter link or folder is sent via GET you will get a undefiend index warning for
$link=$_GET["link"];
$folder=$_GET["folder"];

Posted: Wed Jul 04, 2007 5:18 am
by afsin
volka wrote:If no parameter link or folder is sent via GET you will get a undefiend index warning for
$link=$_GET["link"];
$folder=$_GET["folder"];

But I do not get any warning, it is working. If no paramater is sent into php file, shows firstpage.php.

Posted: Wed Jul 04, 2007 5:20 am
by volka
Then either error_reporting isn't set to E_ALL and/or display_errors is off.