Sunday, August 1st, 2010

Conditional Statements

Monday, October 13th, 2008 by Peter Zhang

This article is a continuation from What is PHP?. if you have not already read that post, please point yourself to that page =)

Today, I am going to briefly talk about conditional statements, such as if… elseif…… those kinds of things. Get yourself ready, and let’s get started!

Let me first start off from completing the challenge on the last post.

You open up a HTML document, put down the neccessary codes such as <html><head><body>, etc. and then now you are going to put some codes in the <body> tag so they will show up. This is what you should have:


<?php
$name = "Peter Zhang";
$age = 15;
$gender = "Male";
$website = "http://www.techcube.net";
?>
<table>
<tr>
<td>Name:</td>
<td><?php echo $name; ?></td>
</tr>
<tr>
<td>Age: </td>
<td><?php echo $age; ?></td>
</tr>
<tr>
<td>Gender: </td>
<td><?php echo $gender; ?></td>
</tr>
<tr>
<td>Website: </td>
<td><?php echo $website; ?></td>
</tr>
</table>

This is a very basic table that has four rows and two columns. The ones in the second column should display what you have set the values to be, which are Peter Zhang, 15, Male and http://www.techcube.net respectively.

Well now, I am going to apply some conditional statements here. Meaning, making something happens only when something else is true. Conditional Statements plays a big part in the programming world, in fact, I think it would be safe to say almost everything is made up of conditional statements.

How do you use it? It’s actually very simple:


<?php
if ( ) {
//Things that it does if whatever inside if() is true
}
?>

It can be expanded into:


<?php
if ( ) {
//If whatever inside if() is true, this happens
} elseif () {
//If whatever inside if() is false but elseif() is true, this happens
} elseif () {
// You may have any numbers of elseif() as you want
} else {
// if whatever inside if() and any of the elseif() is false, this happens
}
?>

To clarify how exactly it works, refer to this flowchart:

With the if statements, you may add a little warning to the users, such as “You are too young” if the user’s age is below 50; or, warn a user if the name is blank using this code:


<?php
if ( $age < 20 ) {
//if the value of $age is below 20, shows a warning message
echo "You are too young!";
} elseif ( $name == "" ) {
//if the value of $name is blank, shows a warning message
echo "What is your name!?";
} else {
//You can actually quit php tags so that you can edit the HTML part more easily
//if everything is correct, shows the table
?>
<table>
<tr>
....
<?php
// Make sure you close the if statement
} ?>

So now, it checks if the age is less than 20, if it is not, it checks if the name is empty, then if it returns false, it displays the table. Note that I used double equal signs in $name == “”;. One very important concept is that “=” is totally different from “==”. “=” is used when you want to give a variable a value, while “==” is used when comparing two objects. So in the code above, I was comparing $name with “” (blank value). It will return true if $name is blank.

Some of the indicators/symbols that you can use:

&& or AND – and statement- (ex. $age < 18 && $name == “”) – returns true if BOTH of the statements are true
|| or OR – or statement – (ex. $age < 18 || $name == “”) – returns true if EITHER one oft he statements are true
! – neglect – ($name !==””) – returns true if the statement is false, or returns false if the statement is true (in other words, returns the opposite answer)

AND and OR might sound complicated at first, but once you dig into programming, you will slowly understand the concept. Look at this Venn Diagram:

If you pick a point in the middle, both AND and OR will become true. However if you pick a point in the pure red/green area, only OR will return true.

I guess this is what I have today. If you have any questions, feel free to ask us!

Oh, I almost forgot. Today’s challenge – I’ll give you four numerical values in 4 variables $a, $b, $c and $d. Try to figure out which one of them is below 10, which one is below 20 but above 10, which one is below 30 but above 20, and which one is above 30.

Good luck!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Comments

Receive updates on the comments through RSS 2.0!

1 Response to “Conditional Statements”

  1. Anonymous

    October 29th, 2008 at 9:48 pm

    Thanks your message has very much helped me:)

Leave a Reply