Create a website RSS feed

PHP Conditions

PHP conditions

Last update on: 09-24-2008
Maybe you have already seen a script and you've noticed things like,
<?php
if( $var == 'ok')
{
print 'test';

}
else{
print 'refuse';
}
?>
That is what we call a condition. This one runs as follows:
<?php
if($var == 'ok') //if the variable named $var is equal to ok
{
print 'test'; //so we display the result 

}
else{ // else we display another message
print 'refuse';
}
?> 
I think everything is fine now, Take a look at these examples:

Example 1: with if else elseif.

<?php
$variable = 'car';

if($variable == 'car'){

print 'good! you found it';
}
elseif($variable =='bike'){
print 'you are so close!';

}
else {
print 'it\'s not the answer, try again';
} 

Example 2: switch().

<?php
switch($operation)

{
case '1': // If the variable equal to 1
print ' operation number 1'; // display this phrase
break; // we close this case

case '2':// If the variable equal to 2
print 'operation number 2';
break;

default: // If the variable doesn't have any value
print 'operation by default'; // Display something by default
}
?> 
You will understand better later, the advantage of the switch function and the conduct of a script with if else elseif etc ...

The control operators:

==
strictly equal
!=
different
>
more than
<
less than
>=
more or equal to
<=
less or equal to
&&
and
||
or
AND
and
OR
or
TRUE
1 or yes
FALSE
0 or false

The mathematical operators:

+
addition
-
subtraction
/
division
*
multiplication
%
modulo

To your keyboards and start typing and testing.

PHP and MySQL's lessons:

Introduction To PHP
Get Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

Banner HomeServices Contact |  ©2009 http://www.iteachweb.net/