PHP: If

The If fuction if PHP is very similar to that of C:

<?php

$love= 1;

if ($love == 1){

echo "love present \n";

}

echo " we are all humans";

?>

Note, there is no "then" used as in most other programming languages, only curly braces ({}) are used to enclose if statement. The above code would output following result:

love present

we are all humans

Thus, the condition operator on love equal to 1 would result in the condition being true and hence, the output. Also, note that the condition is checked using condition operator (== and not =) as disccussed in the operator chapter.

Now, I would discuss another example with false condition :

<?php

$love= 0;

if ($love == 1){

echo "love present \n";

}

echo " we are all humans";

?>

The aboe example is pretty straight-forward and would output following result:

we are all humans

Because, $love(remember variables in PHP start with $ sign) has been initialized to 0 and in the if operation, we are checking for condition $love== 1 which is false, only "we are all humans" is printed.