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

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

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

ghostrider
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://www.ghostrider.trap17.com

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://www.ghostrider.trap17.com/tut/php6

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:

CODE
<?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 (smile.gif 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.

CODE
<?PHP
print("<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.

 

 

 


Reply

Blessed
Greetings

wow 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:

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : practical, application, switch, statements, 6th, php

  1. Create Dynamic Html/php Pages Using Simple Vb.net Code
    Taking your application data, and creating a webpage for others to vie (1)
  2. Php Form Data And Conditional Statements
    My third tutorial (2)
    Intro To PHP Tutorial 3 - Form Data and Conditional Statements Released 4/12/07 By Chris Feilbach
    aka GhostRider Contact Info: E-mail: assembler7@gmail.com AIM: emptybinder78 Yahoo:
    drunkonmarshmellows Website: http://www.ghostrider.trap17.com PART I - FORM DATA One of the
    things that makes PHP so popular is its ability to handle form data. You can do whatever you want
    with it, you can email it to yourself, store it in a database, display it on a page, you can do
    literally ANYTHING with it. There are two ways to send data to your server, GET and POST. Both
    are ....
  3. How To: Ip Configuration Script (win Xp)
    If you travel a lot with your laptop, and need to switch between diff (0)
    How To: IP Configuration Script (win XP) If you travel a lot with your laptop, and need to switch
    between different IP's in different locations, this script is for you. There are many programs
    that handle this task very well, but they cost sometimes pretty big money. This tutorial will show
    you how to make your own IP Configuration Script for free. This script is for a static ip address
    configuration and is based on a little program called "NETSH.EXE" which is supplied with Windows.
    How it works. QUOTE Microsoft Windows XP - Using Netsh Netsh.exe is a....
  4. Majik Switch
    (0)
    Step 1. Here is the eithernet cable we will be using. Step 2. Strip the main part of the cable so
    that you can see the little wire things (be careful not to cut any of them). Step 3. Separate the
    ORANGE wire (not orange/white one) from the others so it's easy to strip. Step 4. Cut the
    orange wire. Step 5. Strip the orange wire about an inch or so long on both sides. Step 6. Wrap
    one end of the stripped, orange wire around one of the metal parts on your switch (sorry dont really
    know how to explain what to wrap it around to). Step 7. Wrap the other side of the ....

    1. Looking for practical, application, switch, statements, 6th, php

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for practical, application, switch, statements, 6th, php

*MORE FROM TRAP17.COM*
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE