PHP: The Switch Statement and Arrays

In our last exciting adventure (back in early November), we braved crocodiles, ravenous editors, most of the PHP statements, and beginning loops. In this edition we’ll cover the final statement, the Switch, and discuss arrays. So sit back, order your R2D2 robot to bring you a cold, frosty Jolt Cola, and let’s get cracking.

The Switch Statement

If you want to refresh your memory of that last fateful article, point your browser here. Go ahead, I’ll wait.

I remember as a young child, my grandmother would make me go and get a switch from a tree every time I was bad; she would then try to whip some sense into me. And thank God she did. I mean, if she hadn’t I wouldn’t be the Pulitzer Prize, Nobel Laureate winning, oodles-of-fame-and-fortune, Hollywood-wheelin-and-dealin type of writer you see before you today. However, because of that experience, the word switch makes me…well…cringe (I was gonna say twitch, but that would have been horrible). Fortunately for me, I encountered my dearest friend, the Switch Statement, and now all that is in the past.

The Switch Statement saves us fat and lazy programmers time and coding. Instead of writing a billion lines of If statements, we can use this bad boy instead. But enough yapping about it. Let’s see it in action:


<html>

<body>


<?php

$wrestler = “Junkyard Dog”;

echo “My favorite wrestler is $wrestler<br />”;

switch ($wrestler)

{

case “Hulk Hogan”:

echo “Whatcha gonna do when Hulkamania runs wild on you brother?!?”;

break;

case “Big John Stud”:

echo “I’m Big. I’m John. And I’m a stud.”;

break;

case “Macho Man”:

echo “Ewww YEAH! THE…MACH…O…MAN…RANDY SAVAGE and the Beautiful Missus E…liza…beth!”;

break;

case “Junkyard Dog”:

echo “I wear pants that say THUMP on the buttocks.”;

break;

}

?>

The above code creates a variable named $wrestler and stores the value “Junkyard Dog” in it. Next it prints the line “My favorite wrestler is” and places whatever is stored in the $wrestler variable into the sentence. Finally, it uses the Switch Statement to print a response based on the data inside the $wrestler variable. Since we already put the value “Junkyard Dog” in the variable, it will print:

  I wear pants that say THUMP on the buttocks.

If the value were, say, “Hulk Hogan”, it would have printed the following to the screen:

  Whatcha gonna do when Hulkamania runs wild on you brother?!?

And finally, if the value of $wrestler had been anything but the values listed, nothing would have occurred.

But let’s say you wanted something to happen if none of the values matched. For that, we could insert a Default clause, like so:


<html>

<body>


<?php

$wrestler = “Ravishing Rick Rude and Hillbilly Jim”;

echo “My favorite wrestler is $wrestler<br />”;

switch ($wrestler)

{

case “Hulk Hogan”:

echo “Whatcha gonna do when Hulkamania runs wild on you brother?!?”;

break;

case “Big John Stud”:

echo “I’m Big. I’m John. And I’m a stud.”;

break;

case “Macho Man”:

echo “Ewww YEAH! THE…MACH…O…MAN…RANDY SAVAGE and the Beautiful Missus E…liza…beth!”;

break;

case “Junkyard Dog”:

echo “I wear pants that say THUMP on the buttocks.”;

break;

default:

echo “I don’t even watch wrestling. Unless it involves females, midgets, or old men.”;

break;


}

?>

All we did in this code is add the Default clause, and change the value of $wrestler so it doesn’t match any of the values. Now if we ran this program it would print:

  I don’t even watch wrestling. Unless it involves females, midgets, or old men.

{mospagebreak title=The Variable’s Big Brother}

Yeah you heard me. You’d better not mess with the variable, or he’ll get his big brother, the Array, to lay the goon hand down on you. And you don’t want that, because he is bigger, badder, and holds a lot more data.

Arrays in PHP are similar to the arrays in most programming languages. Like a variable, they hold data. Unlike variables, they hold more than one piece of data. They can hold as many as you like, what with their giant, rippling muscles. There are three types of arrays, and we are going to discuss them all here. They are the Numeric Array, the Associative Array, and my favorite, the squeezably soft Multidimensional Array.

Numeric Arrays

The name of the Numeric Array is a little misleading. When I first read it I thought they only stored numbers, which of course is wrong. The Numeric part simply refers to how the values in an array are stored. Other languages were smart enough not to name their arrays Numeric; I don’t know why PHP wasn’t.

The easiest way to think of an array is like a drawer with many file folders in it. Each file within the drawer holds data. If that is confusing, then gaze at my marvelous code, which will enlighten your puny brain:


$mygirlfriends = array(“Your Mother”, “Angelina Jolie”, “Whitney White”);

In the above example, PHP automatically assigns the numeric index to your array. The value in the first position (Your Mother) gets the index of 0, the second position (Angelina Jolie) gets 1, and the third position (Whitney White) gets the index 2.

If we wanted to assign the index number manually, we could also write it this way:


$mygirlfriends[0] = “Your Mother”;

$mygirlfriends[1] = “Angelina Jolie”;

$mygirlfriends[2] = “Whitney White”;

Both ways store the information the same way; which is the best manner to assign the array values is up to you.

{mospagebreak title=Printing the Array Values}

If we wanted to print the values inside of the array, we could do so this way:


<html>

<body>

<?php

$mygirlfriends[0] = “Your Mother”;

$mygirlfriends[1] = “Angelina Jolie”;

$mygirlfriends[2] = “Whitney White”;


echo $mygirlfriends[1] . “and” . $names[2]



}

?>

The above code would print:

  Angelina Jolie and Whitney White

{mospagebreak title=Associative Arrays}

Associative Arrays use an ID key that is not necessarily a numeric value. Here is an example:


$mystats = array(“Weight”=>900, “Looks”=>1, “IQ”=>12);

Another way to create an associative array is like this:


$mystats['Weight'] = 900;

$mystats['Looks'] = 1;

$mystats['IQ'] = 12;

If it still isn’t clear, this following coding example should make it so:


<html>

<body>

<?php


$mystats['Weight'] = 900;

$mystats['Looks'] = 1;

$mystats['IQ'] = 12;


echo “I weigh “ . $mystats['Weight'] . “pounds.”;

}

?>

The above code would result in the following:

  I weigh 900 pounds.

As you can see, we call on the index (in this instance "Weight") to retrieve how much I weigh, which prints the value 900. If I had called on "Looks" it would have printed 1, and so forth. So instead of referencing the values in the array with numbers, we just did it with word labels to make it easier to remember and understand (you wouldn’t label your files in a file cabinet with numbers, unless you were The Count from Sesame Street that is).

{mospagebreak title=The Multidimensional Array}

The most enjoyable array is the multidimensional array. It lets you store an array inside of an array inside of an array and so forth. The best way to explain it is to show it in use, so here we go:


<html>

<body>

<?php

$heroesandvillains = array


(

Super Friends”=>array

(

Superman”,

Wonder Woman”,

Batman”,

The Wonder Twins”,

Aqua Man”,

Black Vulcan”,

Apache Chief”,

Robin”,

Green Lantern”,

Hawkman”,

That Monkey Whose Name I Can’t Remember”

),

Legion of Doom”=>array

(

Lex Luthor”,

Solomon Grundy”,

The Riddler”,

Cheetah”,

Bizarro”,

Black Manta”,

Scarecrow”,

Sinestro”

Gorilla Grodd”,

Brainiac”,

Giganta”,

Toyman”

),

Ancillary”=>array

(

Mr. Mxyzptlk”

)

);

}

?>

The above code creates a multidimensional array named $heroesandvillans and then creates a series of sub-arrays within it (Super Friends, Legion of Doom, and Ancillary). Finally, values were assigned to each sub-array.

Let’s extract some data from the above sample:


<html>

<body>

<?php

$heroesandvillains = array


(

Super Friends”=>array

(

Superman”,

Wonder Woman”,

Batman”,

The Wonder Twins”,

Aqua Man”,

Black Vulcan”,

Apache Chief”,

Robin”,

Green Lantern”,

Hawkman”,

That Monkey Whose Name I Can’t Remember”

),

Legion of Doom”=>array

(

Lex Luthor”,

Solomon Grundy”,

The Riddler”,

Cheetah”,

Bizarro”,

Black Manta”,

Scarecrow”,

Sinestro”

Gorilla Grodd”,

Brainiac”,

Giganta”,

Toyman”

),

Ancillary”=>array

(

Mr. Mxyzptlk”

)

);

echo “Oh no it’s “ .$superheroesandvillains['Ancillary'][0] . “!”;


}

?>

If printed, this code would print:

  Oh no it’s Mr. Mxyzptlk!

{mospagebreak title=Beyond Multidimensional Arrays}

You can also create arrays that are two-dimensional, three-dimensional, and so on. You could even have a fifth-dimensional array. If you don’t want to kill yourself though, or some unfortunate programmer who has to come along and read your code in the future, then you may just want to max your arrays to the third-dimension.

Here is a sample of a quick two-dimensional array:


<html>

<body>

<?php


$soda = array( array(Type=>“Mountain Dew”,

Price=> 1.25,

Quantity=> 20)

),

array(Type=> “Pepsi”,

Price=> 1.25,

Quantity=> 22,

),

array(Type=>“Mello Yello”,

Price=>1.25,

Quantity=> 30

)

);

?>

In the above example we use Associative arrays inside our two-dimensional array to create the column names. Otherwise we would have to use numbers, and things would get really ugly.

Well, that’s it for this tutorial. In the (eventual) next episode we will go over Loops inside of PHP. So come back often.

Till then…

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri