PHP Switch/else if/ternary operator

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

PHP Switch/else if/ternary operator

Post by dzcadii » Sun Mar 01, 2015 6:40 pm

Here is a simple Switch in PHP. If you are tired of:

if(){}else{}elseif(){}

Code: Select all

<?
$MyName=$_GET['MyName'];

if($MyName==""){
echo "Blank";
}
elseif($MyName=="Something"){
echo $MyName;
}
else{
echo "Please give me a name";
}
?>
switch(){case:}

Code: Select all

switch($MyName){
case $MyName=="":
echo "Blank";
//break will stop the execution of the switch only. The rest of the script will run
break;
case $MyName=="Something":
echo $MyName;
break;
}
?>
Ternary operator
This is the same as if(){}else{}

Code: Select all

<?
$First = "Dz";
$last = "SoundNirvana";
$name = ($First == "Dz" && $last == "SoundNirvana") ? "DzSoundNirvana" : "I am DzSoundNirvana.com";
?>

Post Reply