Hi

I've quite a while into my PHP course now however I seem to have missed something, I keep seeing things like:

if ($variable_name) {..

It's quite scary because I should really know this and it seems extremely basic so sorry for the questions simplicity.
Is it asking wether the statment is true?
If the variable exists?

Thankyou in advanced

Recommended Answers

All 3 Replies

The variable exists and is not set to one of these:
* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)
...

if($variable) {
//statements
}

This simply means, if(true) . If a variable is set and if doesn't have values 0,false,'', it will execute corresponding statements.
Example,

<?php
$a = NULL;
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}

$a="";
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}

$a=1;
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}

$a=0;
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}

$a=true;
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}

$a=false;
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}

$a=100;
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}

$a=array("10","20");
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}

$a = "some string";
if($a) {
 echo "$a is true<br />";
} else {
echo "$a is false<br />";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.