Search
Left Quote    A clever person solves a problem. A wise person avoids it.
- Einstein    
Right Quote
 
[login] | [Register]
 

Simple PHP Tutorial

by: Jester
Page: 4 of 9
(View All)

Conditionals, if/else, Elseif


We can perform actions in PHP based on whether or not a conditional statement is true. Let's take a look at an example.




Look at it logically: "If two is greater than one". The > operator means "greater than". The conditional statement is contained with the brackets following the if() function. If this statement is true, the PHP code between the curly braces, { } will be executed. Two will always have a greater value than one, so this statement will always be true.




Now here we say "If one is greater than two, print out so. Or ELSE, print out "one is not greater than two".


Operators

There are many operators you can use in PHP, and some come in handy for conditionals.




We can use these operators in our conditional statements. If a statement is TRUE, the if() function will execute the functions contained within the curly braces, if it is NOT true, it wont. We can optionally include an else statement after the if statement. But it doesn't stop there.


elseif

Take a look at the code below and see if you can figure out what's happening.



PHP will test if each condition is true, if one is, it will execute the code between that statements curly braces. If none of them are true it will execute the code contained within the else statement. Remember the else statement is optional, if we don't want anything to happen if no conditions are true, we can simply leave this out.

Logic: If $number is greater than 10, print out so. ELSE if $number is less than 5, print out so. ELSE if $number is greater than 5, print out so. ELSE print out the code between the else {} curly braces.

So what will actually happen here? Well none of the statement are true, 5 is not greater than 10, or less that 5, or greater than 5. The else statement will be executed. Let's use some strings:




Not too difficult, I'm sure you'll agree. Conditional statements are essential, as you'll find out if you decide to use PHP.


Embedded Example:

I'm assuming, if you're reading this you're familiar with HTML, so let's put the above to a practical use. Let's ask the user for their name:




PHP is case sensitive, i.e Bill is not the same as bill, Bob is not the same as bob.

Take your time and look through the above script. Notice we can jump in and out of PHP mode whenever we like, even within a conditional statement's curly braces, print out the HTML we want to, and then open PHP mode again and close the conditional's curly braces, then proceed with the next conditional:




If we didn't jump out of PHP mode, we would have to do:


If an echo statement that uses double quotes contains double quotes within it, we must "escape" them using the special "" escaping character. Personally, i find this very annoying to type out, especially when outputting alot of HTML, it's much easier to close PHP mode and type out the HTML as normal, using the <?=$variable?> shortcut to output any PHP variables.

All the script does is check if $name has been entered, if it hasn't, it displays a form asking them to enter their name. If $name has been entered it executes the if, elseif, else statements contained within the else statement. If the name is Bill it tells him he is the author, else if the name is Bob it tells him he is a member, or else it tells the person they are a visitor.


I think that's enough on conditionals, go on to control structures.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

235,175 Views
1 Total Comments
3.25 Rating of 5 (4 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank