p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Switch statement...


Message #1 by jazzsnob <jazzsnob@o...> on Sun, 20 Oct 2002 23:01:47 -0400
hello all. i was wondering if you would help me with a switch statement.

i was wondering if you could test values using a "!=" for a switch,
something like this:

switch(!=$page_log_id) {
	case 'news':
		header('Location: http://localhost/test.html?page_log_id=news');
		exit();
		break;
		case 'biography':
		header('Location: http://localhost/test.html?page_log_id=biography');
		exit();
		break;
}

i can only get the statement to work with  "equal to" values, but i need it
for "!=" values. anyone have any ideas?


Message #2 by Anonymous on Mon, 21 Oct 2002 12:34:15
try something like this:


switch (true) { 

case (X != 1): 

case (Y != 1): 

default: 
}



-Anonymous



> hello all. i was wondering if you would help me with a switch statement.

i was wondering if you could test values using a "!=" for a switch,
something like this:

switch(!=$page_log_id) {
	case 'news':
		header('Location: http://localhost/test.html?
page_log_id=news');
		exit();
		break;
		case 'biography':
		header('Location: http://localhost/test.html?
page_log_id=biography');
		exit();
		break;
}

i can only get the statement to work with  "equal to" values, but i need it
for "!=" values. anyone have any ideas?


Message #3 by jazzsnob <jazzsnob@o...> on Mon, 21 Oct 2002 09:55:03 -0400
thanks for the reply. the values are string values though. it is an id for
pages on a website. this way i can log if someone is coming from a seach
engine or something, anywhere from outside the site.

there are 8 possible values plus "". if not "", then what is it. and i want
to make sure nothing but those eight values are used since they show up in
the address bar and people can enter their own. so i check for them and
redirect them to the page if the url value gets typed into.

switch($page_log_id) {
	case ($page_log_id != "page1":
		//something
}

that is an example of what i'm trying to do, i know it doesn't work. but
maybe someone could clue me into what to do?

thanks again...



-----Original Message-----
From: Anonymous
Sent: Monday, October 21, 2002 8:34 AM
To: beginning php
Subject: [beginning_php] Re: Switch statement...


try something like this:


switch (true) {

case (X != 1):

case (Y != 1):

default:
}



-Anonymous



> hello all. i was wondering if you would help me with a switch statement.

i was wondering if you could test values using a "!=" for a switch,
something like this:

switch(!=$page_log_id) {
	case 'news':
		header('Location: http://localhost/test.html?
page_log_id=news');
		exit();
		break;
		case 'biography':
		header('Location: http://localhost/test.html?
page_log_id=biography');
		exit();
		break;
}

i can only get the statement to work with  "equal to" values, but i need it
for "!=" values. anyone have any ideas?




Message #4 by "KayKay-GG \(E-mail\)" <kaykay@g...> on Mon, 21 Oct 2002 23:00:04 +0530
It doesn't matter that it's a string. Anonymous's code should still solve the
problem...

Your original code:

switch(!=$page_log_id)
{
	case 'news':
		header('Location: http://localhost/test.html?page_log_id=news');
		exit();
		break;
	case 'biography':
		header('Location: http://localhost/test.html?page_log_id=biography');
		exit();
		break;
}

That won't work...

But this will:

switch(true) {
	case ($page_log_id != 'news'):
		header('Location: http://localhost/test.html?page_log_id=news');
		exit();
		break;
	case ($page_log_id != 'biography'):
		header('Location: http://localhost/test.html?page_log_id=biography');
		exit();
		break;
}

So all you're doing is checking if the case conditions are equal to true...
-K
P.S credit should go to Nik for this idea :)

>>>>-----Original Message-----
>>>>From: jazzsnob [mailto:jazzsnob@o...]
>>>>Sent: 21 October 2002 19:25
>>>>To: beginning php
>>>>Subject: [beginning_php] Re: Switch statement...
>>>>
>>>>
>>>>thanks for the reply. the values are string values though.
>>>>it is an id for
>>>>pages on a website. this way i can log if someone is coming
>>>>from a seach
>>>>engine or something, anywhere from outside the site.
>>>>
>>>>there are 8 possible values plus "". if not "", then what
>>>>is it. and i want
>>>>to make sure nothing but those eight values are used since
>>>>they show up in
>>>>the address bar and people can enter their own. so i check
>>>>for them and
>>>>redirect them to the page if the url value gets typed into.
>>>>
>>>>switch($page_log_id) {
>>>>	case ($page_log_id != "page1":
>>>>		//something
>>>>}
>>>>
>>>>that is an example of what i'm trying to do, i know it
>>>>doesn't work. but
>>>>maybe someone could clue me into what to do?
>>>>
>>>>thanks again...




  Return to Index