w3resource

PHP if / else / elseif statement

Description

The if statement execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false. For this purpose else is used.

Syntax:

if (condition)
   execute statement(s) if condition is true;
else
  execute  statement(s) if condition is false;

Example :

<?php
$overtime=60;
if ($overtime<=50)
{
$pay_amt=1200;
$medical=1000;
echo "Pay Amount : $pay_amt : Medical : $medical";
}
else
{
$pay_amt=2000;
$medical=1500;
echo "Pay Amount : $pay_amt : Medical : $medical";
}
?>

As we have initialized the $overtime value as 60, therefore the else statement will be executed.

View the example in the browser

PHP: elseif statement

Description:

elseif is a combination of if and else. It extends an if statement to execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false.

The following example display 'x is greater than y', 'x is equal to y' or 'x is smaller than y' depends on the value of $x or $y.

Example :

<?php
if ($x > $y) 
{
echo "x is bigger than y"; 
}
elseif ($x == $y)
{
echo "x is equal to y";
} 
else
{
echo "x is smaller than y";
}
?>

PHP: else statement

Description:

The if statement execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false. For this purpose else is used.

Syntax:

if (condition)

   execute statement(s) if condition is true;
  else
  execute  statement(s) if condition is false;

Example:

<?php
$overtime=60;
if ($overtime<=50)
{
$pay_amt=1200;
$medical=1000;
echo "Pay Amount : $pay_amt : Medical : $medical";
}
else
{
$pay_amt=2000;
$medical=1500;
echo "Pay Amount : $pay_amt : Medical : $medical";
}
?>

As we have initialized the $overtime value as 60, therefore the else statement will be executed.

View the example in the browser

PHP: elseif statement

Description:

elseif is a combination of if and else. It extends an if statement to execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false.

The following example display 'x is greater than y', 'x is equal to y' or 'x is smaller than y' depends on the value of $x or $y.

Example :

<?php
if ($x > $y) 
{
echo "x is bigger than y"; 
}
elseif ($x == $y)
{
echo "x is equal to y";
} 
else
{
echo "x is smaller than y";
}
?>

Previous: Incrementing Decrementing Operators
Next: while statement



PHP: Tips of the Day

Concatenation Operators: You can use concatenation to join strings "end to end" while outputting them (with echo or print).

You can concatenate variables using a . (period/dot).

Example:

<?php
// String variable

$name = 'Jhon';

// Concatenate multiple strings (3 in this example) into one and echo it once done.

echo '<p>Hello ' . $name . ', Nice to meet you.</p>';

// Concatenation Operators
?>

Output:

<p>Hello Jhon, Nice to meet you.</p>

Similar to concatenation, echo (when used without parentheses) can be used to combine strings and variables together (along with other arbitrary expressions) using a comma (,).

<?php
$itemCount = 1;
echo 'You have learn ', $itemCount, ' Tips', $itemCount === 1 ? '' : 's';
?>

Output:

You have learn 1 Tips

String concatenation vs passing multiple arguments to echo

Passing multiple arguments to the echo command is more advantageous than string concatenation in some circumstances. The arguments are written to the output in the same order as they are passed in.

echo "The total is: ", $x + $y;

The problem with the concatenation is that the period . takes precedence in the expression. If concatenated, the above expression needs extra parentheses for the correct behavior. The precedence of the period affects ternary operators too.

echo "The total is: " . ($x + $y);