PHP Tutorial 4: Control Statements

by -
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

An entrepreneur by birth, blogger by choice, and geek by heart. He founded Sprouts Media, a blogs network company which currently own over 10 popular web properties, to cater his passion of blogging and entrepreneurship. He is also known as an avid reader, technology enthusiast, explorer, and a broken lover. His passion for knowledge keeps him running all the time. He loves to write about Technology and Social issues on his blogs.