Skip to main content

Our today post will focus on how to create php swith case

Php compare different color and return the match

What is switch case in php? To make it simple: switch case is usually a method of comparing two objects to decide true or false, negative or positive action. In php script below we will set a variable container with value color and compare it with different color and see which could match the previous value. Copy the php script below your testing.

<!--php $color = "red"; switch ($color) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; default: echo "Sorry, you didn't select any color!"; } ?-->

Php switch case to compare digit number and return the match

Another method of comparison is setting up a digit number and compare it with another digit number. While running the below code you can change different digit number to the activity of switch case comparison. Now, you can copy the code below or write it manually for your testing.

$d = 5; switch ($d) { case 1: echo "Your favourite number is: 1"; break; case 2: echo "Your favourite number is: 2"; break; case 3: echo "Your favourite number is: 3"; break; case 4: echo "Your favourite number is: 4"; break; case 5: echo "Your favourite number is: 5"; break; case 6: echo "Your favourite number is: 6"; break; case 7: echo "Your favourite number is: 7"; break; case 8: echo "Your favourite number is: 8"; break; case 9: echo "Your favourite number is: 9"; break; case 10: echo "Your favourite number is: 10"; default: echo "Sorry, you didn't select any choice\nPlease select any choice!"; }

Summary

Some time we can't guarrantee you that every script you found on this website is error free, but incase if any code spot error, don't hesitate to contact us so that we would discuss it. Thanks for reading our articles.

Comments