Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Fresh Tuts Project 62 AOM Designs Pixel Designz 

Change Language

Who's online

There are currently 0 users and 7 guests online.

Lesson 7 - Control Structures

IF

Today we will go over simple control structures. You can think of them as "forks in the road" of your code. Depending on whether the control structures are true or false will determine what your code does next. Here is a simple example of a control structure:

<?php
if (code true) {
 
then do statement
}
?>

"If" checks to see if the expression is TRUE and if it is: the "if" will run the code below it. In other words - "If" an expression evaluates to TRUE, PHP will execute the following statement, and if it evaluates to FALSE - it'll ignore it.

The following example would display "2 is bigger than 1" (2 > 1) if 2 IS bigger than 1:

<?php
if (1) { // if "2" is greater than (>) "1" (Which it is! :P )
print "2 is bigger than 1";
}
?>

However, the following "if" statement would not do anything because it is FALSE:

<?php
if (2) { //If "1" is greater than "2". 
//FALSE because 1 is smaller than 2!
print "1 is bigger than 2"
}
?>

Quote:
Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of [the] C [coding language]. - PHP.net

<?php
if (expression true) {
 
then do statement
}
?>

Now lets make a simple script that checks a number to see if it is to big, or if it is to small. Open your text editor (if you don't have one use Notpad) and save the blank file as "if.php". Now write the following text in the file:

<?php
/* Make a new variable called "$cpu_number" 
and set it equal to a random number between 0 and 100. */
$cpu_number rand(0100);

// Make a new variable called "$your_number" and set it equal to "50".
$your_number 50;

 If (
$your_number $cpu_number) { 
 
/* "if" $your_number is greater than $cpu_number do the following: */
 
echo "Your Number Is Bigger! CPU number is $cpu_number";
 }
 
 If (
$your_number $cpu_number) {
 
/* "if" $your_number is less than $cpu_number do the following: */
 
echo "Your Number Is Smaller! CPU number is $cpu_number";
 }
?>

When you run the code, you will ether see "Your Number Is Bigger!" or "Your Number Is Smaller", depending on wither the computer chose a number bigger or smaller than yours. Run the script several more times to make different outcomes and you can also change the code to say different things! If you want you can add a third "if" that says something if both numbers are the same (If ($your_number == $cpu_number)).

Now lets make it so that a user can enter a number and see if they have a bigger number. Change the code in "if.php" to the following:

<form action="if.php" method="get"> <!--//Make the HTML Form -->
Your Number:<input name="num" type="text"> <!--//Make the input box -->
<input type="submit" value="Submit"> <!--//Make the Submit button -->
</form> <!--// End the Form -->


<?php
/* Make a new variable called "$cpu_number" 
and set it equal to a random number between 0 and 100. */
$cpu_number rand(0100);

/* "if" something was posted (i.e. isset in 'num') do the following: */
if (isset($_GET['num'])) { 
   
   
$number $_GET['num']; 
   
/*put the posted variable "num" into a new variable called "$number". */
     
   
If ($number $cpu_number) {
       
/* "if" $number is greater than $cpu_number do the following: */
       
echo "Your Number Is Bigger! CPU number is $cpu_number";
    }
     
    If (
$number $cpu_number) {
       
/* "if" $number is less than $cpu_number do the following: */
       
echo "Your Number Is Smaller! CPU number is $cpu_number";
    }

}

?>

When you save the file and upload it to your server you will be presented with a form where you can enter a number and see if it is higher or lower than the number the CPU chooses.

You will notice in the above example there are two "if" statements inside of another "if" statement:

<?php
if (isset($_GET['num'])) { 
   
$number $_GET['num']; 
    If (
$number $cpu_number) {
        echo 
"Your Number Is Bigger!";
    }
    If (
$number $cpu_number) {
        echo 
"Your Number Is Smaller!";
    }
}
?>

If the first statement is TRUE PHP will go on to process the next two "if" statements. If the first "if" statement is FALSE PHP will ignore the other two "if" statements inside of the first. That is why you don't see anything when you first run the script - the first if is FALSE until you post a number!

Not Equal Too?

It sounds like someone needs speech lessons! Actually, that is how this next "operator" reads.

<?php
if ($variable is NOT EQUAL to $variable2) {
   
then do statement
}
?>

Open backup up you text editor and make a new file called "lesson7.php". Then type the following into it:

<?php
// set "$cpu_number" to a random number between 0 and 3
$cpu_number rand(03);
// Set $your_number equal to "1"
$your_number 1;

If (
$your_number != $cpu_number) { 
   
/* "if" $your_number is not equal to $cpu_number do the following: */
   
echo "You have a different NUMBER! CPU number is $cpu_number";
}
 
If (
$your_number == $cpu_number) {
   
/* "if" $your_number is equal to $cpu_number do the following: */
   
echo "Your number is the same as the CPU's number!";
}
?>

Run the page a couple of times to see the output of both if statements.

Other PHP Operators

PHP actually has lots of operators besides ==, !=, . Here are some of them:

<?php
if ($variable is less than or equal to $variable2) {
   
then do statement
}
if (
$variable <= $variable2) {
   
then do statement
}
?>

<?php
if ($variable OR $variable2 is equal to $variable3) {
   
then do statement
}
if (
$variable || $variable2 == $variable3) {
   
then do statement
}
?>

Note: In an OR expression if either of the variables are true, the statement is true - or if both are true, the statement is true! However, if you only want the statement to be true if BOTH are true, then you need to use the AND (&&) operator:

<?php
if ( ($variable is equal to $variable3) AND ($variable2 is equal to $variable3) ) {
   
then do statement
}
if ( (
$variable == $variable3) && ($variable2 == $variable3) ) {
   
then do statement
}
?>

Don't worry about memorizing these operators right now. As we use them in future scripts you will get the hang of them.

For more information please read these articles:

Using If Else Statements
Operators
Looping The Loop
If and Switch
Simple Loops

If you find a broken link please PM me, thanks!

Watch Part 1 of Lesson 7

Watch Part 2 of Lesson 7


Submitted by David on November 9, 2006 - 6:08pm.
printer friendly version

What is the difference

What is the difference between "echo" and "print"?


Echo and Print

Well, if you don't know the difference then there won't be any for you. In other words; the difference is so small that unless you are so deep in code that you know the difference - don't worry about it...

But I use print when using ob_start(); and echo otherwise.


Thank you

Thank you for share your knowledge.


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati