Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> A Practical Application Of Get And Switch Statements, My 6th PHP tutorial
ghostrider
post Apr 21 2007, 01:12 AM
Post #1


Super Member
*********

Group: Members
Posts: 397
Joined: 9-June 06
From: Wisconsin
Member No.: 24,924



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.

This post has been edited by ghostrider: Apr 21 2007, 02:29 PM
Go to the top of the page
 
+Quote Post
Blessed
post Apr 21 2007, 07:08 PM
Post #2


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



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:
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Error Not A Valid Win32 Application(10)
  2. Stupid Statements(13)
  3. Opengl And Mfc Dialog Based Application(7)
  4. How Do I Switch From Gnome To Kde?(6)
  5. How To Install An Application As A Service(4)
  6. Detailed C Beginner Tut(2)
  7. Valve Hammer(1)
  8. The Power Of Java(14)
  9. 250 Credits For One Year Of Hosting Suggestion(14)
  10. Gps Watch - Mobile Application(4)
  11. What Made You Switch To Linux?(32)
  12. Best Way To Lock A Folder Without Using An Application(13)
  13. Network Outage. Distribution Switch Ddosed.(11)
  14. Web Hosting Application [denied](1)
  15. Free Webs Hosting Application [screened] [approved](2)
  1. Free Web Hosting Application [screened] [approved](2)
  2. Chappill's Hosting Application. [screened] [approved](2)
  3. Free Web Hosting Application [denied](1)
  4. Hosting Application [denied](1)
  5. Hosting Application [denied](1)
  6. Gfx Crew Application(1)
  7. New Gfx Crew Application(0)
  8. ~.madlock.~ Application(2)
  9. Free Web Hosting Application [denied](2)
  10. Application Form [approved](2)
  11. Free Web Hosting Application [screened] [approved](6)
  12. Free Web Hosting Application [screened] [approved](3)
  13. Hosting Struts Application(1)


 



- Lo-Fi Version Time is now: 25th July 2008 - 10:28 PM