AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL
HTML Tutorial

PHP If, else and elseif


<< Previous Page
PHP Operator Types
Next Page >>
PHP Switch

PHP Tutorial » If, else, elseif statement in PHP - examples

Conditional PHP statements
Very often, when writing code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

if statement - executes some codes if a condition is true
if ... else statement - executes a certain code if a condition is true and another code if that condition is false
if ... elseif ... else statement - executes different codes for more than two conditions
switch statement - selects one of the many blocks of code to execute.

PHP - If statement


The if statement executes a code if a condition is true.

Syntax:
if (state) {
code to be executed if the condition is true;
}


Example 1: Create if statement:
<? php
$t = date("H");
if ($t <"20") {
echo "Have a good day!";
  }
?>

PHP - The if ... else statement


The if ... else statement executes one code if one condition is true and another code if that condition is false.

Syntax
if (state) {
code to be executed if the condition is true;
} else {
code to be executed if the condition is false;
}


Example 2: Create if ... else statement:
<? php
$t = date("H");
if ($t <"20") {
echo "Have a good day!";
} else {
echo "Good night!";
}
?>

PHP - The if ... elseif ... else statement


The if ... elseif ... else statement executes different codes for more than two conditions.

Syntax
if (state) {
code to be executed if this condition is true;
} elseif (stand) {
code that must be executed if the first condition is false and this condition is true;
} else {
code to execute if all conditions are false;
}

Example 3: Create if ... elseif ... else statement:
<? php
$t = date("H");
if ($t <"10") {
 echo "Good morning!";
} elseif ($t <"20") {
echo "Good day!";
} else {
 echo "Good night!";
}
 ?>

<< Previous Page
PHP Operator Types
Next Page >>
PHP Switch



PHP if, else and elseif Statements - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.