PHP Switch Control

Switch statement is used to perform different actions based on differnt conditions.

Switch Statement

<?php

$day=strtoupper(date("D"));
switch($day){
	case "MON":
		echo "Today is Monday.";
		break;
	case "TUE":
		echo "Today is Tuesday.";
		break;
	case "WED":
		echo "Today is Wednesday.";
		break;
	case "THR":
		echo "Today is Thursday.";
		break;
	case "FRI":
		echo "Today is Friday.";
		break;
	case "SAT":
		echo "Today is Saturday.";
		break;
	case "SUN":
		echo "Today is Sunday.";
		break;
	default:
		echo "You entered wrong case!";
	
}
?>
	

Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.

PHP IF…ELSE-IF

Conditional statements are used to perform different actions based on different conditions.

PHP contains following conditional statements

  • if statement
  • if…else statement
  • if…else if…else statement
  • switch statement

IF Statement

Simple if statement contains only one response if condition is true.

<?php

$day=strtoupper(date("D"));
if($day=="FRI")
	echo "Have a Nice Weekend!";

?>
	

IF Else Statement

if else statement contains response for both true & false conditions

<?php
$day=strtoupper(date("D"));

if($day=="FRI")
	echo "Have a Nice Weekend!";
else
	echo "Have a Nice Day!";
?>
	

IF…ELSE-IF…ELSE Statement

IF…ELSE-IF…ELSE is used in multiple conditions scenario.

<?php

$day=strtoupper(date("D"));
if($day=="FRI")
	echo "Have a Nice Weekend!";
else if($day=="SUN")
	echo "Have a Nice Sunday!";
else	
	echo "Have a Nice Day!";

?>
	


PHP While Loops

Loops are used to execute a code till a specific condition/counter.

PHP Supports four type of Loops

  • For Loop
  • While Loop
  • Do…While Loop
  • Foreach Loop

While Loop

While Loop executes the code of block until it’s specified condition comes true.

<?php
	$counter=1;
	while($counter<=5){
		echo $counter."<br>";
		$counter++;
	}
?>
	

While Loop with Break

Break is used to terminate the while loop during block execution.

<?php
	$counter=1;
	while($counter<=5){
		echo $counter."<br>";
		if($counter==3){
			echo "Break at 3 <br>";
			break;
		}
		
		$counter++;
	}
?>
	

Do…While Loop

Do…While Loop works same like while loop but once it will run it’s code block however condition is true or false.

<?php
	$counter=1;
	do{
		echo $counter."<br>";
		$counter++;
	}while($counter<=5);
?>
	

PHP Loops

Loops are used to execute a code till a specific condition/counter.

PHP Supports four type of Loops

  • For Loop
  • While Loop
  • Do…While Loop
  • Foreach Loop

For Loop

For Loop executes a code till the specified number.

<?php
	for($a=0;$a<11;$a++){
		echo $a."<br>";
	}
?>
	

Foreach Loop

Foreach loop only works on arrays. and is used to loop through each key/value in an array

<?php
	$color=array("white","green","black","blue","ornage","pink");
	foreach($color as $value){
		echo $value."<br>";
	}
?>
	

PHP Operators Precedence & Associativity

OperatorAssociativity
clone newNon Associativity
[Left
**Right
!Right
* / %Left
+ – .Left
<< >>Left
< <= > >=Non Associativity
== != === !== <> <=>Non Associativity
&Left
^Left
/Left
&&Left
||Left
??Right
?:Left
= += -= *= **= /= .= %= &= |= ^= <<= >>=Right
andLeft
xorLeft
orLeft

PHP
Logical Operators

Arithmetic Operators are used to Perform Arithmetic Operations on Numeric Values Such as addition, subtraction, multiplication and etc.

OperatorNameExampleResult
andAND$a and $btrue if both $a and $b are true
orOR$a or $btrue if either $a or $b is true
xorXOR$a xor $btrue if either $a and $b is true, but not both true.
&&AND$a && $btrue if both $a and $b are true
||OR$a || $btrue if either $a or $b is true. or both are true.
!NOT!$aTrue if $a is a false

PHP
Comparison Operators

Arithmetic Operators are used to Perform Arithmetic Operations on Numeric Values Such as addition, subtraction, multiplication and etc.

OperatorNameExampleResult
==Equal$a==$breturn true if both $a and $b are equal
===Identical$a===$breturn true if both $a and $b are equal, and both have same data type
!=Not Equal$a!=$breturn true if $a is not equal to $b
<>Not Equal$a<>$breturn true if $a is not equal to $b
!==Not Identical$a!==$breturn true if $a is not equal to $b, or both are from differnt data types.
>Greater then$a>$bReturn true if $a is greater then $b
>=Greater then or equal$a>=$bReturn true if $a is greater then or equal to $b
<Less then$a<$bReturn true if $a is less then $b
<=Less then or Equal$a<=$bReturn true if $a is less then or equal to $b

PHP
Arithmetic Operators

Arithmetic Operators are used to Perform Arithmetic Operations on Numeric Values Such as addition, subtraction, multiplication and etc.

OperatorNameExampleResult
+Addition$a+$bAddition of $a and $b
Subtraction$a-$bDifference of $a and $b
*Multiplication$a*$bProduct of $a and $b
/Division$a/$bQuotient of $a and $b
%Modulus$a%$bRemainder of $a divided by $b
**Exponentiation$a**$bResult of raising $a to the $b’th power (Introduced in PHP 5.6)

PHP
Increment & Decrement Operators

PHP increment operators are used to increment a variable’s value.

PHP decrement operators are used to decrement a variable’s value.

OperatorNameResult
++$aPre IncrementIncrement $a by 1 and then return.
$a++Post IncrementReturn $a and then Increment $a by 1.
–$aPre DecrementDecrement $a by 1 and then return.
$a–Post DecrementReturn $a and then Decrement $a by 1.

PHP
Assignment Operators

PHP assignment operators are used to assign or process with numeric values. The basic assignment operator in PHP is “=”. It means that the left operand gets set to the value of the assignment expression on the right.

OperatorNameExampleResult
=Equal$a = $bAssign the $b value to $a.
+=Addition$a+=$bAdd the value of $b to $a and store the result in $a.
-=Subtraction$a-=$bSubtract the $b value from $a. and store the result in $a.
*=Multiplication$a*=$bMultiply the $a and $b value. and store the result in $a.
/=Division$a/=$bDivide with $a by $b. and store the result in $a.
%=Modulus$a%=$bremainder of $a divided by $b. and store the result in $a.