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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

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

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If $variable evaluates to true the condition is fulfilled.
see http://www.php.net/manual/en/language.t ... an.casting
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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";
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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 "===."
afsin
Forum Newbie
Posts: 3
Joined: Wed Jul 04, 2007 4:35 am

Post 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().
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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"];
afsin
Forum Newbie
Posts: 3
Joined: Wed Jul 04, 2007 4:35 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Then either error_reporting isn't set to E_ALL and/or display_errors is off.
Post Reply