Multiple Switches... with the same criteria

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Multiple Switches... with the same criteria

Post by hypedupdawg »

I have recently been trying to write a section of code that will allow a video to be shown either at it's default size, or at a user value. To do this I have been using two switch statements like this:

Code: Select all

switch ($vidinfo[2])
		{
		case "small":
			$vidwidth = 400;
			break;
		case "medium":
			$vidwidth = 600;
			break;
		case "large":
			$vidwidth = 800;
			break;
		default:
			break;
		}
	switch ($size)
		{
		case "small":
			$vidwidth = 400;
			break;
		case "medium":
			$vidwidth = 600;
			break;
		case "large":
			$vidwidth = 800;
			break;
		default:
			break;
		}
What I want to know is - is there any way to always give these statement the same criteria? I tried this:

Code: Select all

switch ($vidinfo[2])
		{
		include($_SERVER['DOCUMENT_ROOT'] . "/videos/switch.txt");
		}
	switch ($size)
		{
		include($_SERVER['DOCUMENT_ROOT'] . "/videos/switch.txt");
		}
But it throws the error "unexpected T_INCLUDE". Is there any way to do this - maybe using variable variables?
Thanks a lot :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Multiple Switches... with the same criteria

Post by requinix »

How do you determine which one to use? What if $vidinfo[2]=small and $size=medium?
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Multiple Switches... with the same criteria

Post by minorDemocritus »

Almost every time I get the Unexpected T_something error, it's because I've forgotten a semicolon above the line that pops the error... check for that.
wurdup
Forum Commoner
Posts: 39
Joined: Thu Apr 01, 2010 11:36 am

Re: Multiple Switches... with the same criteria

Post by wurdup »

why cant you use a function that's what theyre for. I'm not an expert on PHP security but I dont think it's recommended using include for a txt file.

Code: Select all

$vidwidth=400;

check($size);
check($othervar[2]);

function check($var)
                {
                switch($var)
                case "small":
                        $vidwidth = 400;
                        break;
                case "medium":
                        $vidwidth = 600;
                        break;
                case "large":
                        $vidwidth = 800;
                        break;
                default:
                        break;
                }


        
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: Multiple Switches... with the same criteria

Post by hypedupdawg »

thanks for everyone's great response... thanks for reminding me of the (ever present) function option - it works great now with roughly the same syntax as wurdup suggested.

In answer to tasairis, I used an if statement to use the $size variable only if it was present:

Code: Select all

if ($size != "")
		{
		$vidwidth = choosesize($size);
		}
	else
		{
		$vidwidth = choosesize($vidinfo[2]);
		}
I'll look in the other forums for the security facts :D thanks to all of you
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Multiple Switches... with the same criteria

Post by requinix »

The issue is that you're misusing switch. It has a very specific form and you have to stick to it.

But yeah, stick it in a function.
And also, putting PHP code in a .txt file, especially one that's accessible over the web, is a very bad idea. Stick to .php.
Post Reply