Forum Moderators: coopster

Message Too Old, No Replies

if, elseif - can it be as an array?

         

smallcompany

12:01 am on Jun 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,

I extract the referring page name ($page value) which I want to code, rather then passing on the long names. I have this:

if ($page == "page1")
{
$s4= "01";
} elseif ($page == "page2") {
$s4 = "02";
} elseif ($page == "page3") {
$s4 = "03";
} else {
$s4 = $page;
}// end of IF

I set the page codes manually, and those that were not set, the full name will get passed at the end ($s4 = $page;).

Now I wonder if this could be setup as an array, something that would look like this:

$s4 = array(
"page1" => "01",
"page2" => "02",
"page3" => "03"
);

Would that be possible? If it would, how do I create the last statement to pick the page that was not on the list?

Thank you

Tommybs

7:00 am on Jun 15, 2010 (gmt 0)

10+ Year Member



Hi,

I think you might be able to use in_array();

if( in_array($page, $s4){
//do something
}else{
//do default
}

coopster

1:36 pm on Jun 15, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or use isset(). Have a look at this recent discussion for an example ...

when an include() goes inside an if(). [webmasterworld.com]

code junkie8

7:55 pm on Jun 16, 2010 (gmt 0)

10+ Year Member



If you're doing like $_GET['page'] or something, the switch function is a lot better to use.

(Note, I'm just listing a different method that's possible).

switch ($page) {
default:
// home page or 404 error (error handling)
break;

case "about":
// run code for your about page
break;

case "contact":
// run code for your contact page
break;
}


?>

Just a small example to help you out - it's what I use and have always used, however if you're handling MANY pages, your array method should work and then include that array page or whatever you're doing.

(Sorry, I tend to babble on).