free classified ads and leads
domain website price


Switch vs Match in PHP

PHP

Switch vs Match in PHP

Switch Statement

Basic syntax of a switch statement :

switch (expression)
{

case value1:
 // code to execute if expression equals value 1
 break;
case value2;
 //code to execute if expression equals value 2
 break;
default:
 // code to execute if none of the above cases match
}

Example :

$dayofweek =4;
switch($dayofweek)
{
case 1:
echo "its monday";
break;
case 2:
echo "its tuesday";
break;
case 3:
echo "its wedneshday";
break;
case 4:
echo "its thursday";
break;
case 5:
echo "its friday";
break;
default:
echo "its the weekend";

}

Match Expression

Basic syntax of a match statement:

$result = match ( expression)
{
value1 =>expression1, value2 => expression2,  // additional cases

default=>expression_default

};


$dayofweek = 4;
$result=match ($dayofweek)
{
1=>"its monday",
2=>"its tuesday",
3=>"its wednesday",
4=>"its thursday",
5=>"its friday",
default=> "its the weekend"
}


echo $result;   /// its thursday

 

Type Safety

The match expression in PHP is more type-safe compared to the switch statement. In a switch statement, loose type comparisons can lead to unexpected behavior, but the match expression enforces strict type checking. A match arm compares values strictly ( === ) instead of loosely as the switch statement does.

 

$value=1;
switch ($value)
{

case 1:
echo "integer 1";
break;
case "1":
echo "string '1'";
break;

}

// output integer 1


$value ="1";

$result=match($value){ 
1=>"integer 1",
"1"=>"string '1'",
};

echo $result;

// output : string '1'

 

"Fall through " Behavior

In a switch statement, if you forget to use break after a case, execution "falls through" to the next case , potentially causing unintended behavior. The match expression doesn't have this issue.

Exhaustiveness checking

the match expression enforces exhaustive checking, meaning you need to cover all possible cases. If you miss a case, PHP will generate a warning or error.

 

Bookmark and Share




Gallery

Related Topics

Comments



Submit Reviews:


(Enter the characters seen on the image below.)



Promotional Ads

Advertisement

Packers and Movers in Chandigarh
Plot No 636, Near HP Gas Agency, Dariya, Chandigarh - 160101
Plot No 636, Near HP Gas Agency, Dariya, Chandigarh - 160101
Kurukshetra

Packers and Movers in Chandigarh
Plot No-123 Sunrise Colony Godown Area Pabhat Zirakpur-140603
Plot No-123 Sunrise Colony Godown Area Pabhat Zirakpur-140603
Jind

Packers and Movers In Satna - VP Max
Ground Floor, Shop 2, JL Institute of Study Center, Tilak Ganj, Ward 14, Sagar, Madhya Pradesh 470002
Ground Floor, Shop 2, JL Institute of Study Center, Tilak Ganj, Ward 14, Sagar, Madhya Pradesh 470002
Shahdol

Packers and Movers in Sagar
Ground Floor, Shop 2, JL Institute of Study Center, Tilak Ganj, Ward 14, Sagar, Madhya Pradesh 470002
Ground Floor, Shop 2, JL Institute of Study Center, Tilak Ganj, Ward 14, Sagar, Madhya Pradesh 470002
Sagar

Packers and movers in Mohali
Plot No 636, Near HP Gas Agency, Dariya, Chandigarh - 160101
Mohali

JB Rock Bolts & SN BOLTS
10 A & B Industrial Area (at Vill. Nangal Jarialan)
Distt. Una Himachal Pradesh
Una

Packers and Movers in Zirakpur - Packers and Mover
SCO 3 AKS 2, Bhabat Rd, near Post Office, Zirakpur

Website Design and Development
Bangalore
Hyderabad

JF Engineering Products LLP
Parhar Village, Near Vasai Fata, Eastern Express Highway, Vasai (E), Palghar,
Motilal Nagar Mumbai

Nandishwar Steel
51 M.G. Mahimtura Marg, 3rd Kumbharwada Lane, Opp. Durgadevi Garden
August Kranti Marg Mumbai