PHP Switch Statement

Associate
Joined
16 Jul 2008
Posts
271
Location
London
Hi,

Normally we use the php switch statement to compare values and the isset to check if a variable is set and not null.

Is there anyway to use the switch statement to check if a variable is set.

Currently my code checks if the variable isset then runs a switch statement, can I use the switch statement to do the whole thing?

Example code:

Code:
<?php
//display either 1.video, 2.image, 3.default image
if (isset($default['swfurl'])){
echo "flash video";
} else {
	 switch ($default['imageurl']){
		 case "":
			 echo "<li>no image</li>";
			 break;						
	 default:
		 echo "<li>" . $default['imageurl'] . "</li>";								}
}
?>
 
A switch clause isn't really appropriate for what you want to do, as you have dependence on more than one variable. I'd just use normal if clauses:

PHP:
<?php

if (isset($default['swfurl']))
{
	echo "flash video";
}
else if (!empty($default['imageurl']))
{
	echo "<li>$default[imageurl]</li>";
}
else
{
	echo "<li>no image</li>";
}

?>

You only have three cases, so it doesn't make much difference.
 
Last edited:
Do you really need a switch statement when you are only checking between 2 options and a default? Even if you was to change the whole thing into a switch statement, you would have to have an 'if' within one of the cases to check the imageurl.

The other option is to just us if statements if your goal is to shorten the code a bit.

Code:
if (isset($default['swfurl']))
    echo "Flash Video";
else if (isset($default['imageurl']) && $default['imageurl'] != "")
    echo "<li>" . $default['imageurl'] . "</li>";
else
    echo "<li>no image</li>";

My understanding of switch statements is that you use them when you want to match a variable with a various number of outcomes, so using it here is a bit pointless (someone correct me if i'm wrong :P)


EDIT: Damn, beaten to it xD
 
Last edited:
Thanks for your swift response, seems I am using the switch statement inappropriately and will use the if else statement to achieve this as suggested.

Cheers
 
I sometimes like to use the ternary operator in place of multiple if/else/elseif statements if I think it makes the code either more compact or more readable in it's intent.

I'm not suggesting either of these reasons is true in this case just showing that it's an option. You sometimes find using the ternary operator leads to long lines of code, in which case for readability if/else statements might be the better choice.

PHP:
<?php

if (isset($default['swfurl']))
{
    echo "flash video";
} else {
    $image = !empty($default['image']) ? $default['image'] : 'No image.';
    echo "<li>$image</li>";
}
 
Ternary ifs are a really good thing to know for assigning to variables with more than one potential value.

Its all about using the correct method for the situation. I dont tend to go near switch statements unless there are at least 4-5 cases and its the simplest way of doing things.
 
I sometimes like to use the ternary operator in place of multiple if/else/elseif statements if I think it makes the code either more compact or more readable in it's intent.

I'm not suggesting either of these reasons is true in this case just showing that it's an option. You sometimes find using the ternary operator leads to long lines of code, in which case for readability if/else statements might be the better choice.

PHP:
<?php

if (isset($default['swfurl']))
{
    echo "flash video";
} else {
    $image = !empty($default['image']) ? $default['image'] : 'No image.';
    echo "<li>$image</li>";
}

Fanstastic, amazing what you can learn from asking a simple question
 
Using ternary inside if blocks is just messy.

You also don't need to check isset() when using empty() - it does that, too.
 
Back
Top Bottom