News

PHP Tutorial 4: Control Statements

PHP tutorials

In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false. Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition.

There are four types of conditional statements in PHP

  1. If statement
  2. If-else statement
  3. If-elseif-else statement
  4. Switch statement

Let us explain you each of the above with simple examples.

  • IF-statement

Executes the block of code if the specified condition is true.

Syntax :

If(condition)
{
code;
}

  • If-else statement

If the condition in the if statement  is found false then the code in else block is executed.
Syntax:

if(condition)
{
code;
}
else;
{
code;
}

  • If-elseif-else statement

Is used when more than one block of code needs to be checked and executed.

 Syntax:

if (condition)
{
code;
}
elseif (condition)
{
code;
}
else
{
code;
}

Let us show you a best example on all the three control statements which can give a intelligence to your website or application.
Example:

<html>
<body>
<h1> Simple welcome note </h1>
<?php
$d=date(“D”);
if ($d==”Fri”)
echo “Have a nice weekend!”;
elseif ($d==”Sun”)
echo “Have a nice Sunday!”;
else
echo “Have a nice day!”;
?>
</body>
</html>

 PHP tutorials

Also see

  • PHP Tutorial 1: Introduction for Beginners
  • PHP Tutorial 2: PHP Script Syntax and HTML File Compatibility
  • PHP Tutorial 3: Variables in PHP
Comments
To Top