Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does if($variable) work? [duplicate]

Tags:

php

Possible Duplicate:
Regarding if statements in PHP

In PHP scripts - what does an if statement like this check for?

<?php if($variable){ // code to be executed } ?>   

I've seen it used in scripts several times, and now I really want to know what it "looks for". It's not missing anything; it's just a plain variable inside an if statement... I couldn't find any results about this, anywhere, so obviously I'll look stupid posting this.

like image 395
Nisto Avatar asked Jul 14 '11 13:07

Nisto


People also ask

How do you check if two variables are true?

The is keyword is used to test if two variables refer to the same object. The test returns True if the two objects are the same object. The test returns False if they are not the same object, even if the two objects are 100% equal. Use the == operator to test if two variables are equal.

What does if variable mean in PHP?

It checks whether $variable evaluates to true . There are a couple of normal values that evaluate to true , see the PHP type comparison tables. if ( ) can contain any expression that ultimately evaluates to true or false .

How can check Boolean value in if condition in PHP?

The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.

How do you check if a variable is equal to a string in PHP?

The is_string() function checks whether a variable is of type string or not. This function returns true (1) if the variable is of type string, otherwise it returns false/nothing.


2 Answers

The construct if ($variable) tests to see if $variable evaluates to any "truthy" value. It can be a boolean TRUE, or a non-empty, non-NULL value, or non-zero number. Have a look at the list of boolean evaluations in the PHP docs.

From the PHP documentation:

var_dump((bool) "");        // bool(false) var_dump((bool) 1);         // bool(true) var_dump((bool) -2);        // bool(true) var_dump((bool) "foo");     // bool(true) var_dump((bool) 2.3e5);     // bool(true) var_dump((bool) array(12)); // bool(true) var_dump((bool) array());   // bool(false) var_dump((bool) "false");   // bool(true) 

Note however that if ($variable) is not appropriate to use when testing if a variable or array key has been initialized. If it the variable or array key does not yet exist, this would result in an E_NOTICE Undefined variable $variable.

like image 69
Michael Berkowski Avatar answered Oct 12 '22 04:10

Michael Berkowski


If converts $variable to a boolean, and acts according to the result of that conversion.

See the boolean docs for further information.

To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

like image 21
Mat Avatar answered Oct 12 '22 04:10

Mat