Loading

PHP If…Else Statements

Submitted by: 
Language: 
Visitors have accessed this post 8940 times.


PHP If…Else Statements

The if statement is used to execute code with condition. If the value of the condition is True then execute the statement else execute another statement or simply end the execution.

There are three combination of the If statement namely:

  • if statement – only one condition is executed.
  • if...else statement – only one condition is executed but it will execute another statement if the condition is false.
  • if...elseif....else statement – it can contain more than two conditions and execute a statement if the condition is true otherwise execute the statement using the else.

The if statement

Execute the code if the condition is true.

If Syntax

  1. if (condition) statement to be executed if condition is true;

or

  1. if (condition)
  2. {
  3. statement to be executed if condition is true;
  4. Another statement to be executed if the condition is true;
  5. }

The first syntax is used if only one line of code is to be executed after the condition.

The second syntax is used to execute multiple lines of code.

Consider the following example for the first syntax:

  1. <?php
  2. $month=date("M");
  3.  
  4. if ($month=="Aug") echo "The month is August!";
  5. ?>

Second syntax example:

  1. <?php
  2. $month=date("M");
  3.  
  4. if ($month=="Aug")
  5. {
  6. echo "The month is August!";
  7. echo "Have a nice day!";
  8. }
  9. ?>

The if…else statement

The above code will execute only if the condition is true. The If…Else statement is able to execute another statement if the condition is false.

If…Else Syntax

  1. if (condition)
  2. statement to be executed if condition is true;
  3. else
  4. statement to be executed if condition is false;

The sample below will output the correct month if the condition is false:

  1. <?php
  2. $month=date("M");
  3.  
  4. if ($month=="Sep")
  5. {
  6. echo "The month is September!";
  7. }else{
  8. echo "The month is " . date("F");
  9. }
  10. ?>

The function date with “F” parameter will simply output the month in full textual representation like January or February.

Take note that in order to execute multiple statements you must enclosed it with curly braces.

The if...elseif....else Statement

Use this statement if you need more condition aside from single condition.

if…elseif…else statement

  1. if (condition)
  2. statement to be executed if condition is true;
  3. elseif (condition)
  4. statement to be executed if condition is true;
  5. else
  6. statement to be executed if condition is false;

The example code below will try to execute the next condition if the first condition is false. Otherwise execute the statement in the “else” statement.

  1. <?php
  2. $month=date("M");
  3. if ($month=="Sep")
  4. echo "The month is September!";
  5. elseif ($month=="Oct")
  6. echo "The month is October!";
  7. else
  8. echo "The month is " . date("F");
  9. ?>



Comments

partho2mail's picture

I am a new php lerner. And i learn from here.

form,if...elseif....else Statement more information so on

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
To prevent automated spam submissions leave this field empty.