PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>Beginners> PHP Conditionals (if statements)

PHP Conditionals (if statements)

FROM: AUTHOR: TIME:2008-06-02 HITS:
Probably in every PHP script, you will use conditionals, from if statements to switch statements they are everywhere, let's get right into it and start you off with a simple if statement, probably the most used conditional.
PHP:
  1. <?php
  2. if (1 == 1) {
  3.     echo '1 really does equal 1!';
  4. }
  5. ?>

Alright, this is probably the easiest if statement you will ever see, it doesn't even have an else part to it. If you can't understand this (which I hope you can) then I will explain it, all it's doing is saying if 1 is equal (using 2 equal signs is how you define equal to) to 1 then continue, and all we do is echo out a simple statement. You can do anything though, you don't have to echo. Then we end the statement. Now let's use the same thing but add on an else, so if it doesn't equal one we have somewhere to go.

phpsu


PHP:
  1. <?php
  2. if (1 == 1) {
  3.     echo '1 really does equal 1!';
  4. } else {
  5.     echo 'Aww, 1 doesn\'t equal 1.';
  6. }
  7. ?>

As you can see, all we did was add on } else { then add another echo statement. Pretty straight forward, but let's say we are using variables, we can make another tiny bit more complicated example.

do you kown phpsu.com?

PHP:
  1. <?php
  2.  
  3. $number = 1;
  4. $answer = 2;
  5.  
  6. if ($number == $answer) {
  7.     echo '1 really does equal 1!';
  8. } else {
  9.     echo 'Aww, 1 doesn\'t equal 1.';
  10. }
  11. ?>

We use variables in this example, we have a number variable ($number) and an answer variable ($answer), we define static data to these variables, meaning they don't change, but that will be different when you start getting into MySQL and such. www.phpsu.com

Then instead of comparing 2 numbers in our if statement, we compare our 2 variables. Pretty much the exact same thing, except now our variables both contain different numbers so our output will then be Aww, 1 doesn't equal 1. because our 2 variables did not equal each other, so we go to our else statement.

do you kown phpsu.com?

This is just the basics of if statements, but this is also just a basic PHP tutorial. If you want to learn more about if statements check out any of the tutorials on this site, chances are they use if statements in them.

phpsu提供的php教程

Now let's check out switch conditionals, these are in theory the same as if statements except they allow you to have a lot of different answers to them. Here, I will show you a simple example first - www.phpsu.com


PHP:
  1. <?php
  2. $var = 'Cows';
  3.  
  4. switch ($var) {
  5.     case 'Dogs';
  6.         echo 'You chose dogs!';
  7.         break;
  8.     case 'Cats';
  9.         echo 'You chose cats!';
  10.         break;
  11.     case 'Cows';
  12.         echo 'You chose cows!';
  13.         break;
  14.     case 'Birds';
  15.         echo 'You chose birds';
  16.         break;
  17.     default:
  18.         echo 'You didn\'t choose any animals!';
  19.         break;
  20. }
  21. ?>
At first glance, this may look a bit overwhelming, but break it into parts in your mind. Let's assume that we have a form where a person chooses a favorite animal, and then there chose is saved into a variable called $var. From there we output an answer depending on what they choose. First we define that we are starting a switch, we want to then put in what we want to check, then we have each case, a case is what we are trying to match with our switch, if our switch and one of our cases match we continue on with that case, if not we keep checking through them. If our switch matches none of our cases, then we have a default, from there we just put out any old answer that you want your user to see
TITLE:PHP Conditionals (if statements)
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:59:05 GMT-8 2008