How To Change A Switch Statement Output Into A Variable Using Php
The 'switch' statement is used in PHP to implement provisional statements like the 'if-elseif-else' statement. The purpose of the 'switch' statement is to execute the particular statement(s) based on the matching condition from the multiple conditions. Simply some comparisons that can be implemented by the 'if-elseif-else' can't exist implemented by the 'switch' statement. The uses of the 'switch' statement in PHP have shown in this tutorial.
Syntax:
switch (due north) {
instance label1:
// Code to exist executed if n=label1
break ;
instance label2:
// Code to be executed if n=label2
suspension ;
...
default :
// Lawmaking to exist executed if n is dissimilar from all labels
}
The value of the north volition be checked with each case label value and the statement(southward) of the example block will be executed where the value of the example characterization matches with n. The break statement is used to terminate from the switch block if the lucifer is found.
Pre-requisite
The scripts used in the examples of this tutorial are written based on the PHP 8 version. Do the following task earlier executing the examples of this script.
- Install apache2 and PHP 8.
- Set execute permission for all files and folders under /var/world wide web/html folder where all PHP files volition exist stored.
Dissimilar Uses of Switch Argument
Unlike uses of the switch statement in PHP have been shown in this part of this tutorial past using multiple examples.
Example-1: Simple use of switch..instance statement
Create a PHP file with the following script to check the simple use of the 'switch' statement. Here, the $plant variable is used to check the variable $book_name matches with any case value or not.
<?php
//Initialize the book proper name for checking
$book_name = 'PHP & MySQL Novice to Ninja' ;
$found = True ;
//First of switch block
switch ( $book_name ) {
//Cheque the first case value
example "Caput Showtime PHP & MySQL" :
$author = "Lynn Beighley & Michael Morrison" ;
$publisher = "O'Reilly" ;
break ;
//Check the second instance value
case "PHP & MySQL Novice to Ninja" :
$writer = "Tom Butler & Kevin Yank" ;
$publisher = "SitePoint" ;
break ;
//Cheque the third example value
case "PHP: A Beginner's Guide" :
$writer = "Vikram Vaswani" ;
$publisher = "McGraw-Hill Education" ;
suspension ;
//Set the $constitute to false
default :
$constitute = False ;
}
if ( $found == True )
repeat "<b>Book:</b>$book_name<br/><b>Author:</b>$writer<br/><b>Publisher:</b>$publisher<br/>" ;
else
echo 'The book information non found.' ;
Output:
The post-obit output will appear after executing the to a higher place script. Here, the filename is switch1.php that is stored inside /var/www/html/lawmaking binder.
http://localhost/code/switch1.php
Instance-2: Utilise of multiple example statements together
Create a PHP file with the following script to check the way of using the 'switch' statement by combining multiple case statements. Here, the value of $ID volition be assigned by reading the URL query parameter named 'id'. The value of $ID will be checked past the switch..case statement later on to generate the output.
//Cheque the ID value has passed in the URL or not
if ( isset ( $_GET [ 'id' ] ) )
$ID = $_GET [ 'id' ] ;
else
$ID = '' ;
//Define the swith block
switch ( $ID ) {
//Set aforementioned information for three ID values
example '07967' :
example '07895' :
example '07342' :
$text = ' is the ID of a CSE student.' ;
break ;
//Set up information for a particular ID value
example '04523' :
$text = ' is the ID of a English student.' ;;
break ;
//Set same data for two ID values
instance '01876' :
case '01907' :
$text = ' is the ID of a BBA pupil.' ;
suspension ;
//Ready bulletin for empty ID value
default :
$text = '<heart><h3>No Matching data constitute.</h3></eye>' ;
}
repeat "<center><h3>$ID $text</h3></center>" ;
Output:
The following output will appear after executing the to a higher place script without any query parameter. Hither, the filename is switch2.php that is stored inside /var/www/html/code folder.
http://localhost/code/switch2.php
The following output volition announced later executing the higher up script with the query parameter named "id" and the value is "07895" that matches with the first case cake.
http://localhost/code/switch2.php?id=07895
The following output will announced after executing the to a higher place script with the query parameter named "id" and the value is "01907" that matches with the third instance cake.
http://localhost/code/switch2.php?id=01907
Instance-3: Use of Logical 'OR' operator in switch..instance argument
Create a PHP file with the following script to cheque the way of using 'switch' statement in which the 'case' argument is defined by Logical OR operator. Here, the value the of $proper name volition be assigned by reading the URL query parameter named 'proper noun'. The value of $proper noun will be checked by the 'switch..case' statement afterwards to generate the output.
<?php
//Cheque the name value has passed in the URL or not
if ( isset ( $_GET [ 'proper name' ] ) )
$name = strtoupper ( $_GET [ 'name' ] ) ;
else
$proper noun = '' ;
//Ascertain the swith block with Truthful
switch ( True )
{
instance ( $proper noun == "ABIR" ) :
repeat "<center><h3>$name is the squad leader.</h3></center>" ;
suspension ;
//Lucifer any of the three values using logical OR
example ( $name == "RIYA" || $proper noun == "RESMA" || $name == 'MAHBUB' ) :
repeat "<center><h3>$name is a squad fellow member.</h3></middle>" ;
intermission ;
case ( $name == "KARIM" ) :
echo "<heart><h3>$name is the organizer.</h3></center>" ;
interruption ;
default :
repeat "<center><h3>No data found.</h3></middle>" ;
}
?>
Output:
The following output will announced after executing the to a higher place script with the query parameter named "name" and the value, "riya". Here, the filename is switch3.php that is stored inside /var/www/html/code folder.
http://localhost/code/switch3.php?name=riya
The following output will appear afterward executing the above script with the query parameter named "proper noun" and the value, "karim".
http://localhost/code/switch3.php?name=karim
Example-4: Use of the array in switch..case statement
Create a PHP file with the following script to check the manner of using the 'switch' statement in which the 'case' statement is defined the particular array. Here, three arrays have been declared in the script with three types of values. the value of $var will exist assigned by reading the URL query parameter named 'var'. The value of $var will be checked by the 'switch..case' statement subsequently by using the loop to generate the output.
<?php
//Declare three types of assortment
$array1 = [ x , 23 , 56 , 32 , 90 , 11 ] ;
$array2 = [ 7.9 , 3.six , 7.8 , 2.ix , 6.3 , ane.two ] ;
$array3 = [ 'volume' , 'pen' , 'pencil' , 'scale' ] ;
//Cheque the value of the var has passed in the URL or not
if ( isset ( $_GET [ 'var' ] ) )
$val = $_GET [ 'var' ] ;
else
$val = '' ;
//Define the switch block
switch ( truthful ) {
//Check in the get-go array
instance in_array ( $val , $array1 ) :
repeat "<br/><center><p><b>$val</b>exists in the first assortment.</p></center>" ;
break ;
//Check in the 2nd array
case in_array ( $val , $array2 ) :
repeat "<br/><eye><p><b>$val</b>exists in the 2d array.</p></center>" ;
pause ;
//Check in the 3rd array
instance in_array ( $val , $array3 ) :
repeat "<br/><heart><p><b>$val</b>exists in the third assortment.</p></center>" ;
suspension ;
default :
echo "<br/><center><p>The value does non exist in whatsoever array.</p></center>" ;
}
Output:
The following output will appear after executing the above script with the query parameter named "var" and the value, "32". Here, the filename is switch4.php that is stored inside /var/world wide web/html/lawmaking folder.
http://localhost/code/switch4.php?var=32
The following output will appear after executing the in a higher place script with the query parameter named "var" and the value, "2.9".
http://localhost/code/switch4.php?var=ii.nine
The post-obit output will announced afterwards executing the in a higher place script with the query parameter named "var" and the value, "pencil".
http://localhost/code/switch4.php?var=pencil
Decision
The switch..case statement is a useful provisional statement for many programming tasks. The way to check the value of the elementary variable, utilize the logical operator with the variable and use of array with the variable in the switch..case statement has described in this tutorial.
Video Tutorial
Source: https://linuxhint.com/php-switch-statement/
Posted by: tomlinsonpards1941.blogspot.com
0 Response to "How To Change A Switch Statement Output Into A Variable Using Php"
Post a Comment