If statement structure in PHP

Asked
Active3 hr before
Viewed126 times

9 Answers

structurestatement
90%

Your message has been sent to W3Schools.,switch statement - selects one of many blocks of code to be executed,We just launchedW3Schools videos,In PHP we have the following conditional statements:

$a = 50;
$b = 10; >
{
   echo "Hello World";
}
88%

As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to true, PHP will execute statement, and if it evaluates to false - it'll ignore it. More information about what values evaluate to false can be found in the 'Converting to boolean' section. , The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C: , If statements can be nested infinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program. , Often you'd want to have more than one statement to be executed conditionally. Of course, there's no need to wrap each statement with an if clause. Instead, you can group several statements into a statement group. For example, this code would display a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:

if (expr)
   statement
72%

PHP if-else statement is executed whether condition is true or false.,PHP if statement allows conditional execution of code. It is executed if condition is true.,If statement is used to executes the block of code exist inside the if statement only if the specified condition is true.,If-else statement is slightly different from if statement. It executes one block of code if the specified condition is true and another block of code if the condition is false.

12 is less than 100
load more v
65%

The if...else statement,The if...elseif....else statement,In this tutorial you'll learn how to write decision-making code using if...else...elseif statements in PHP.,There are several statements in PHP that you can use to make decisions:

< ? php
$d = date("D");
if ($d == "Fri") {
   echo "Have a nice weekend!";
} ?
>
load more v
75%

The if statement executes a statement if a condition evaluates to true.,It’s possible to nest an if statement inside another if statement as follows:,The following example shows how to nest an if statement in another if statement:,The following example uses the if statement that executes multiple statements:

The if statement allows you to execute a statement if an expression evaluates to true. The following shows the syntax of the if statement:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   } < ? php

if (expression)
   statement;
Code language: HTML, XML(xml)
load more v
40%

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";
} ?
>

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";
} ?
>

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";
} ?
>

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";
} ?
>
load more v
22%

When we want to execute some code when a condition is true, and some other code when that condition is false, then we use the if...else pair.,When we want to execute some code when a condition is true, then we use if statement.,When we want to execute different code for different set of conditions, and we have more than 2 possible conditions, then we use if...elseif...else pair.,While writing programs/scripts, there will be scenarios where you would want to execute a particular statement only if some condition is satisfied. In such situations we use Conditional statements.

Syntax:

if (condition) {
   // code to be executed if 'condition' is true
}
load more v
60%

if (expression) statement,if (expression){ statement 1 statement 2 },if is the keyword we can say in the PHP language like any other programming language. The expression is a conditional statement. The statement is just an instruction that will be executed when this if condition will be true. There is no need to use the braces for a single line of the statement.,Else if Statement in C?

Syntax:

if (expression)
statement

if (expression)
   statement
load more v
48%

Code group is a group of statements which needed to be executed if the condition is true.,Condition is an expression which returns a Boolean value. Here you can use the operators we learned in the last chapter.,if statement executes the code group inside {}, if the condition inside () returns true.,if-elseif-else statement allows you to execute different code groups based on different conditions.

if Syntax

if (condition) {
   code group
}

PHP If Statement Example

<
? php
$day = date('j'); // day of the month
if ($day < 15) {
   echo 'You are spending the first half of the month';
}

PHP If-Else Statement

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

PHP If-Else Statement Example

<
? php
$day = date('j'); // day of the month
if ($day < 15) {
   echo 'You are spending the first half of the month';
} else {
   echo 'You are spending the last half of the month';
}

if-elseif-else statement allows you to execute different code groups based on different conditions.

if (condition 1) {
   code to be executed
   if the condition 1 is true
}
elseif(condition 2) {
   code to be executed
   if the condition 1 is false, but condition 2 is true
} else {
   code to be executed
   if both condition 1 and 2 are false
}

PHP If-Elseif-Else Statement Example

<
? php
$day = date('j'); // day of the month

if ($day >= 21) {
   $quarter = 'last';
} else if ($day >= 14) {
   $quarter = 'third';
} else if ($day >= 7) {
   $quarter = 'second';
} else {
   $quarter = 'first';
}

echo 'You are spending the '.$quarter.
' quarter of the month';

PHP Using Multiple Elseif

<
? php
$randomScore = rand(0, 4); // random score between 0-4
if ($randomScore === 0) {
   echo '0 Points, please try again';
}
elseif($randomScore === 1) {
   echo '1 Point, Try more';
}
elseif($randomScore === 2) {
   echo '2 Points, Nice!';
}
elseif($randomScore === 3) {
   echo '3 Points, One more to reach the best';
}
elseif($randomScore === 4) {
   echo '4 Points, You won!';
}

In many places, you may need to compare the same expression (or variable) with different values. In this case, The switch statement is seriously essential.

switch (expression) {
   case value1:
      code to execute
      if expression = value1
      break;
   case value2:
      code to execute
      if expression = value2
      break;
      ...
   default:
      code to execute
      if expression is not equal to any value above
}

PHP Switch Example

<
? php
$randomScore = rand(0, 4); // random score between 0-4
switch ($randomScore) {
   case 0:
      echo '0 Points, please try again';
      break;
   case 1:
      echo '1 Point, Try more';
      break;
   case 2:
      echo '2 Points, Nice!';
      break;
   case 3:
      echo '3 Points, One more to reach the best';
      break;
   case 4:
      echo '4 Points, You won!';
      break;
}

PHP AND Example

<
? php
$a = true;
$b = true;
if ($a and $b) {
   echo 'Both $a and $b are true';
}

$a = false;
$b = true;
if ($a and $b) {
   echo 'This is not echoed because $a is false';
}

PHP && Example

<
? php
$a = true;
$b = true;
if ($a && $b) {
   echo 'Both $a and $b are true';
}

$a = false;
$b = true;
if ($a && $b) {
   echo 'This is not echoed because $a is false';
}

PHP OR Example

<
? php
$a = true;
$b = false;

if ($a or $b) {
   echo 'Either $a or $b (or both) is true';
}

PHP || Example

<
? php
$a = true;
$b = false;

if ($a || $b) {
   echo 'Either $a or $b (or both) is true';
}

PHP XOR Example

<
? php
$a = true;
$b = false;

if ($a xor $b) {
   echo 'Either $a or $b is true. But not both';
}

$a = true;
$b = true;

if ($a xor $b) {
   echo 'This is not echoed because both $a and $b are true';
}

PHP ! (NOT) Example

<
? php
$a = false;
if (!$a) {
   echo '$a is false. So, !$a is returns true';
}

$a = true;
if (!$a) {
   echo 'This is not echoed because $a is true';
}

PHP Nested If Example

<?php
$a = 10;
if ($a >= 0) {
	echo 'Positive Number <br>';
	if ($a % 10 === 0) {
		echo 'The number is a multiple of 10';
	}
} else {
	echo 'Negative Number. Please enter a positive one';
}

load more v

Other "structure-statement" queries related to "If statement structure in PHP"