WarDrome Blog

PHP Guide: the if statement

PHP Tutorial: The if statement

Hi, today -as mentioned in previous post- we will talk about the if statement.

The if statement is useful when we need to check at a given point in a specific function, if things go as we planned .. but let’s see a practical example:

This value is an integer, for more information about data types, visit the post http://blog.wardrome.com/2011/05/browser-game-school-getting-started-with-php/
$var = 1;

if (is_integer ($ variable)) {
    echo '$variable is an integer!';
} Elseif (is_string ($ variable)) {
    echo '$variable is a string!';
Else {}
    echo '$variable is neither a number nor a string';

This example of course, will always produce the result '$variable is an integer' as the variable $viariabile is, precisely, declared as integer. The elseif instruction can be translated to "If it is not an integer, and if is_string (ie if a string) print $variable is a string." The parameter else instead, you can think of it as "If it is neither complete nor string then write '$variable is neither a number nor a string'.
To practice, try to change the value of $ number and set it as a string for example:

$variable = "I am a string";
/ / [... Rest of code ...]

The result will always be supplied '$variable is a string!'.

Comparison operators

Comparison operators are, in fact, to compare two types of data within an if statement and will provide a Boolean result (true or false).

== What is meant is equal to [value]
! = Is used to indicate other than [value]
=== Is used to indicate SAME [value] (that is equal and the same type, such as two integers)
> What is meant is greater than [value]
> = Is used to indicate greater or equal to [value]

Here are some examples that will describe each operator:

$value = 5;
if ($ value == 5) { echo '$value equals to 5!'; }
if ($ value! = 0) { echo '$value is not 0!';}
if ($ value === 5) { echo '$value is the same as 5!'; }
if ($ value > 1) { echo '$value is greater than 1!'; }
if ($ value> = 3) { echo '$value is greater than or equal to 3!'; } / / ​​in this case will be greater than 3}
if ($ value < 6) { echo '$value is less than 6!'; }
if ($ value <= 6) { echo '$ value is less than or equal to 6!'; } / / ​​in this case will be less than 6}

Important: Due to the weak identification of the PHP language, we must be very careful when you declare a viariabile enclosed in quotes '' or "." In fact, considering the following example:

$value = 5;
$value2 = "5";
if ($value == $value2) {
   echo 'The two variables are equal ";
else {
   echo 'The two variables are not equal';

Even if $value at the time of his statement is an integer, and $value2 is a string (enclosed in quotes because ") the result obtained by this instruction will be" The two variables are equal ", this is because PHP automatically converts the value in $string to maintain control of the 2 values.
Consider instead another example:

$value = 5;
$value2 = "5 people at my table";

if ($value == $value2) {
   echo "The two variables are equal";
else {
   echo "The two variables are not equal";

You're probably thinking that the result of the previous statement will be "The two variables are not equal" ... but is not so. This is because PHP automatically converts the variable $value2 in whole to compare it to $value, removing everything that is not an integer. So the value of $value2 in the if statement will be 5, not "5 people at my table."

But instead if we consider the following example:

$value = 5;
$value2 = "5 people at my table";

if ($value === $value2) {
   echo "The two variables are identical;
else {
   echo "The two variables are not identical;

The above example will result in "The 2 variables are not identical" and that's because you used the control operator (===) which, as explained above, check whether the two variables are identical (both in value type).

Logical operators

Logical operators allow us to concatenate two or more Boolean expressions, so that we can control one or more data within the same line (without repeating if {} {} elseif {} elseif {}). Logical operators are four, and they are:

OR or | | - Check if at least one of the two operators is true, you can use OR or || (double pipe)
AND or && - Check if both operators are true, you can use AND or && (the ampersand)
Xor - Check if only one of two operators is true while the other must be false.
! - The exclamation point is tantamount to a denial, it is true when the value is false or vice versa.

If the above list can be confusing, let's consider this example:

$variable = 14; / / It's an integer
if (is_integer($variable) && $variable > 12 && $variable !== 0) {
   echo "It 's an integer, is greater than 12 and is not 0";

In the previous example, we check if $variable is an integer, if greater than 12, and if it is different from 0. Note that to view the message (echo) all instructions provided must be true.
For practice, try to write this code:

$variable = 11;
if (is_integer($variable) && $variable > 12 && $variable !== 0) {
   echo "It 's an integer, is greater than 12 and is different from 0;
else {
   echo "Failed";

If you try to run the above example you will notice that the message returned is "Failed" because the condition "$variable> 12" will not be true (because $variable is less then 11 to 12).

Let us now analyze the case OR (or ||), taking as inspiration the example above.

$variable = 0;
if (is_integer($variable) || $variable > 12 || !is_string ($variable) {
   echo "It meets at least one condition!"
else {
   echo "Do not even satisfy a condition!"
Running this code, the variable conditions $variable > 12 and !is_string($variable) will return false, but the condition is_integer($variable) will return true then the message will be displayed "It meets at least one condition. " To see the opposite case, we simply change the value of $variable as follows:
$variable = "string";
//[... Rest of code ...]

In so doing, all the conditions will return false because $ variable will not be an integer, greater than 12 and it is no different from a string (!is_string).

On the logical operators is a speech too large and complex to be able to write here, my advice is to practice to test the different results.

In the next lesson we will talk about the switch and to perform repeated cycles. See you next time!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Optionally add an image (JPEG only)

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close