0

Possible Duplicate:
Regarding if statements in PHP

this is a simple question. But Here I need to know how if condition work with only one variable.

$category='';

if ($category) {

}

can you tell what actually check in If condition? Condition has only one variable..

is it checking variable is TRUE or FALSE?

Community
  • 1
  • 1
TNK
  • 4,263
  • 15
  • 58
  • 81
  • 1
    Yes It is checking TRUE or FALSE. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. – Roopendra Jan 14 '13 at 12:42
  • _Every_ expression is (sooner or later) evaluatable to a boolean value and `if` expects one ;) – KingCrunch Jan 14 '13 at 12:53
  • Thanks for all answers... I understood clearly. Thanks again. – TNK Jan 14 '13 at 13:16

8 Answers8

3

PHP is a weak typed language. To understand what is evaluated in the if condition, see the conversion rules for booleans.

Quoting from the manual:

When converting to boolean, the following values are considered FALSE:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

Therefore, your condition will be evaluated as FALSE, since $category == '' and (bool) '' === FALSE

Community
  • 1
  • 1
gd1
  • 11,300
  • 7
  • 49
  • 88
2

Type 1

$category = '';
if ($category) {
   echo 'category';
} else {
   echo 'no category';
}

// Output : no category

Type 2

$category = TRUE;
if ($category) {
   echo 'category';
} else {
   echo 'no category';
}

// Output : category

Type 3

$category = '';
if (!empty($category)) {
   echo 'category';
} else {
   echo 'no category';
}

// Output : no category

Type 4

$category = 0;
if (!empty($category)) {
   echo 'category';
} else {
   echo 'no category';
}

// Output : no category

Type 5

$category = 0;
if (isset($category)) {
   echo 'category';
} else {
   echo 'no category';
}

// Output : category

Dino Babu
  • 5,814
  • 3
  • 24
  • 33
1

this will check for TRUE

if ($category) {

}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
1

You empty string will be casted to a boolean value, false in this case. See the manual on Booleans.

jeroen
  • 91,079
  • 21
  • 114
  • 132
1

This checks whether variable evaluates to true, it's an equivalent of:

if( (bool)$category === true) )
lafor
  • 12,472
  • 4
  • 32
  • 35
1

Yes It is checking TRUE or FALSE. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it.

Roopendra
  • 7,674
  • 16
  • 65
  • 92
1

if ($category) {

}

Will simply check if $category has a value. You did not give $category a value. In this case, it will give a FALSE.

woodle
  • 59
  • 5
  • 1
    Not true, example: `$category = '0'`. It has a value but `if($category)` will still evaluate to `false`. – MrCode Jan 14 '13 at 12:45
0

The block of the if-condition activates whenever the evaluated statement is true.

The empty string in PHP evaluates to false, so this will not be activated. You could be more specific by specifying what you expect, for example:

   $category = '';
   if (empty($category)) {

   }

... in case you expect this to activate whenever it is empty. It really depends on what you are trying to to but like this I assume the condition is never met.

cessor
  • 1,028
  • 11
  • 16