Contents

PHP tutorials

Introduction

Syntax

Variables

Data Types

Operators

echo

If/Else

Loops

chmod()

mail()

date()

phpinfo()

 

 

If/Else Statements

If we want to sensibly respond to our user's input, our code needs to be able to make decisions.  In most programming languages we can use an if statement to make a decision. This if statement must have a condition to use. If the condition is true, following code will be executed.

<?php

$number = 45;

if ( $number == 45 ) {
	echo "This will be executed. ";
             echo "True";
} else {
	echo "This will not be executed. ";
             echo "False";
}
?>

 

In this example we compared a variable to a value. In this case we use ==, which in English means "Equal To".

We can build more complicated logical processes by nesting if statements within each other.

<?php

$number = 45;

if ( $number == 45 ) {
	echo "Our value is equal to 45";
} else {
	if ( $number > 45 ) echo $number;
             if ( $number < 45 ) echo $number;
}
?>