JV’s Power Tips for PHP (2) - The Problem (Page 2 of 7 )
We all want to get things done and there are many ways to skin a cat. For example: Imagine you were writing a live chat script and you wanted to give the user the option of formatting each submission with any combination of Bold, Italic, Underlined and/or Strike Through. Here is an example HTML form for such a script:
At its most basic we can test if the user has selected bold by doing this:
<?php
if ( $_POST['bold'] ) { echo "The user selected bold"; }
?>
Just in case you don't know why this works. It is because if the bold checkbox was selected, then the value of 1 will be sent to PHP (within the variable $_POST['bold'])., but if the bold checkbox was not selected then the variable $_POST['bold'] will not be sent to PHP at all.
Furthermore, in PHP you are able to put a single variable into an if statement. The if statement will then evaluate the contents of the variable. It will evaluate to false under the following conditions:
The variable does not exist
The variable exists with a value of nothing
The variable exists with a value of 0
The variable exists with a boolean value of false
The variable exists with a value of NULL
It will evaluate to true under the following conditions:
The variable exists and has ANY value except empty, 0, NULL or false.
You can also evaluate an assignment within an if statement. Like so:
<?php $i_am_false = false;
if ( $make_me_false = $i_am_false ) { echo "This will never be output!"; } else { echo ‘Because $make_me_false was assigned the value of false'; } ?>
You can also put an array or an object within an if statement.