Get support from Xavier Media
It is currently Sun Dec 08, 2013 12:27 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Lesson 7 - Control Structures
PostPosted: Thu Jul 13, 2006 7:09 pm 

Points:
IF

Today we will go over simple control structures. You can think of them as "forks in the road" of your code. Depinding 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] <?php
if (expression = true)
then do statement
?>[/php]


"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] <?php
if (2 > 1) // if "2" is greater than (>) "1"
print "2 is bigger than 1";
?>[/php]



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



[php] <?php

if (1 > 2) {
/* (if "1" is greater than "2"). FALSE because 1 is smaller than 2
*/
print "1 is bigger than 2";
}
?>[/php]





More About "if":

"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 C:" -PHP.net


[php] <?php
if (expression)
then do statement
?>[/php]











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" (with the quotes). Now write the following text in the file:




[php] <?php
/* Make a new variable called "$cpu_number" and set it equal to "34". */
$cpu_number = 34;
/* Make a new variable called "$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!";
}

If ($your_number < $cpu_number) {
/* "if" $your_number is less than $cpu_number do the following: */
echo "Your Number Is Smaller!";
}

}
?>[/php]



When you run the code you will see "Your Number Is Bigger!" on the screen because the first "if" statment equaled TRUE (50 is greater than 34) while the second "if" was FALSE (50 is less than 34), so only the first statment ran.




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 folowing:



[php]
<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 botton -->
</form> <!--// End the Form -->


<?php
/* Make a new variable called "$cpu_number" and set it equal to "34". */
$cpu_number = 50;

/* "if" somthing was posted (isset) 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!";
}

If ($number < $cpu_number) {
/* "if" $number is less than $cpu_number do the following: */
echo "Your Number Is Smaller!";
}

}

?>[/php]





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 chose.


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



[php] <?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!";
}
}
?>[/php]



If the first statment is TRUE PHP will go on to process the next two "if" statments. If the first "if" statment 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 - it equals FALSE untill you post some value!



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!


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]