March 25th, 2008
Learn PHP From Scratch - Part V
PHP Control Structures
As you’ve read previously, a statement in PHP is just like a sentence in English. 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.
Conditional Statements
One of the most important statement in many languages, PHP included, is the “if” structure. It allows the execution of code, based on different conditions. The if, elseif and else statements in PHP are used to perform different actions based on different conditions.
The PHP if statement is very similar to other programming languages use of the if statement, but for those who are not familiar with it, picture the following:
Think about the decisions you make before you go to sleep. If you have something to do the next day, say go to work, school, or an appointment, then you will set your alarm clock to wake you up. Otherwise, you will sleep in as long as you like!
This simple kind of if/then statement is very common in every day life and also appears in programming quite often. Whenever you want to make a decision given that something is true (you have something to do tomorrow) and be sure that you take the appropriate action, you are using an if/then relationship.
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
* if…else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true
* elseif statement - is used with the if…else statement to execute a set of code if one of several condition are true
The If…Else Statement
If you want to execute some code if a condition is true and another code if a condition is false, use the if….else statement.
Syntax
if (condition) code to be executed if condition is true; else code to be executed if condition is false;
The if statement is necessary for most programming, thus it is important in PHP. Imagine that on January 1st you want to print out “Happy New Year!” at the top of your personal web page. With the use of PHP if statements you could have this process automated, months in advance, occuring every year on January 1st.
This idea of planning for future events is something you would never have had the opportunity of doing if you had just stuck with HTML.
Example
The following example will output “Have a nice weekend!” if the current day is Friday, otherwise it will output “Have a nice day!”:
<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html>
If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces. You don’t have to insert 10 if structure for 10 statements, you just have to group all 10 statements in a statement group using curly braces:
<html> <body> <?php $d=date("D"); if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?> </body> </html>
Another Example
if ($num1 > $num2) { print "The first number is bigger than the second number"; $bigger = $num1; //assigning the value of "$num1" to "$bigger" }
Sometimes you may want to execute a statement if a condition is met, and another statement if the condition is not met. In order to do this, you can use “else” after the first statement, which will execute another statement if the condition resolves to “FALSE”.
if ($num1 > $num2) //is "$num1" bigger than "$num2"? { print "The first number is bigger than the second number"; //if it is, do this $bigger = $num1; //assigning the value of $num1 to $bigger } else { print "The second number is bigger than the first number";//if it’s not, do this $bigger = $num2; //assigning the value of $num1 to $bigger }
Pages : 1 2 3


March 25th, 2008 at 10:53 am
[…] Learn PHP From Scratch - Part V By Jimmy PHP Control Structures As you’ve read previously, a statement in PHP is just like a sentence in English. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty … HeyGB.com - Web Tutorials : Ajax… - http://www.heygb.com […]
May 7th, 2008 at 10:11 am
[…] that does nothing (an empty statement). Statements usually end with a semicolon. In addition, read more | digg […]