Jump to content
xisto Community
Sign in to follow this  
ghostrider

A Practical Application Of Get And Switch Statements My 6th PHP tutorial

Recommended Posts

Intro To PHP
Tutorial 6 - A Practical Application Of GET And Switch Statements
Released 4/20/07
By Chris Feilbach aka GhostRider

Contact Info:
E-mail: assembler7@gmail.com
AIM: emptybinder78
Yahoo: drunkonmarshmellows
Website: http://forums.xisto.com/no_longer_exists/

Today I will teach you another important concept that you probably see a lot in webpages that you visit. Ever see something like "?id=22" or "?mode=login" in your address bar on your browser? If you remember back to the second tutorial you should recognize this as formdata, sent via GET. But you don't need to create a form to utilize this feature. Its really quite simple. This is what we're learning today. The code for today is available at http://forums.xisto.com/no_longer_exists/

This technique is very useful in larger scripts as it keeps the amount of PHP files you need to write very low. That keeps it simple for you, and also for your user. Nobody likes uploading 55 PHP files.

Let's write a PHP site that has 3 links, one that makes the background color red, one that makes it green, and another that makes it blue. Red will be our default.

For sites with one or two or three links like this, conditional statements are perfectly fine. However there are more than one way to be with conditions. The one that I am going to indroduce is called a switch statement. It has a slighty different syntax than if ... then statements, and is better for a value that can have many conditions. It however is sligtly less powerful than an if .;. then statement.

Switch statments give you one condition, which is equal too. If ... then statements allow for equal to, less than, greater than, not equal to, less than or equal to, and greater than or equal to. Switch statements are not good for numbers, unless you know the range of the number, and it always increases in a linear fashion (like 1,2,3,4 or 2,4,6,8).

This is an example of a switch statement:

<?PHP	switch ($var) {	case "yes":	// $var == yes.	break;	case "no":	// $var == no	break;	default:	// This is the choice if no other choice is selected.	}PHP?>

Switch statements require a break; after each case, otherwise PHP will continue executing the code below it, and that causes unpredicted and often bad results. Each case needs a colon (;) afterwards. Only strings need quotes around them. Numbers do not.

The default: is not requirement. If you use one, you may place it anywhere within the switch statement, however it is considered the most correct to place it at the end of your switch statement.

In our example we will use the variable color at the end of the address of our website. Red is our default value. Our other values will be green and blue.

<?PHPprint("<html><head><title>PHP Tutorial 6 Example</title></head>");// Don't print <body> yet, we need that.// As a side note, we need to color our links to make sure the blue doesn't make them invisible.// Save ourselves a lot of typing.  Put the below data in a variable because it will be used 3 times.$data = "<body link='#FFFFFF' vlink='#FFFFFF' bgcolor=";$color = $_GET['color'];	switch ($color) {	case "green":	print("$data'#00FF00'>");	break;	case "blue":	// User wants a blue page	print("$data'#0000FF'>");	break;	default:	print("$data'#FF0000'>");	break;	}// Now print the links.// Save more time, less typing with another variable.$data1 = "<a href='index.php?color=";$data2 = "Click here to turn this page ";print $data1 . "red'>" . $data2 . "red.</a>";print "<br>" . $data1 . "green'>" . $data2 . "green.</a>";print "<br>" . $data1 . "blue'>" . $data2 . "blue.</a>";PHP?>
And there you have it. Next tutorial will cover cookies. Practice your coding and develop some cool stuff.
Edited by ghostrider (see edit history)

Share this post


Link to post
Share on other sites

Greetingswow man.i really like your tutorials man.a lot of explanatin in the tutorials i love them.keep doeing the good work.i'm looking forwardto see more of your php tutorials..Bye :lo:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.