Php developers,Php Tutorials ,Web development, Web Developers, Php hosting, open-source,community
 
Serving the LAMP developers all over the word
LampDevelopers is a pure LAMP Developers community
Php developers,Php Tutorials ,Web development, Web Developers, Php hosting, open-source,community
LAMP Developers
   
   
   
   
   
   
Web Development
   
Developer / Programmer
   
Related Links
   
Member
   
Enter Keyword
Conditional Statements

Conditional Statements:

Over the past pages of our site, we have shown you the basics of text in PHP script and how to store it as variables. In this part of the tutorial we will show you how to use IF statements to make decisions in your PHP scripts.

The Basics Of IF

If these statements are used to compare two values and carry out different actions based on the results of the test condition. If the statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, then the statement is executed for php and mysql web development process. If not, the else statement is executed.

IF Strucure

The structure of an IF statement is as follows:

IF (something == something else)
{
THEN Statement
} else {
ELSE Statement
}

Variables

To the most common phrase use in an IF statement is to compare a variable to another piece of text, a number, or another variable. For example:

if ($username == "webmaster")

This would be compare to the contents of the variable to the text string. The THEN section of code will only be executed if the variable is exactly the same as the contents of the quotation marks so if the variable contained 'Webmaster' or 'WEBMASTER' it will be false.

Constructing The THEN Statment

To add to your script, you can now add a THEN statement:

if ($username == "webmaster") {
echo "Please enter your password below";
}

It will only display this text formate if the username is webmaster. If not, nothing will be displayed. You can actually leave an IF statement like this, as there is no actual requirement to have an ELSE part. This is especially useful if you are using multiple IF statements.

Constructing The ELSE Statement

Adding The ELSE statement is as easy as the THEN statement. Just add some extra code:

if ($username == "webmaster") {
echo "Please enter your password below";
} else {
echo "We are sorry but you are not a recognised user";
}

Of course, we are not limited to just one line of code. We can add any PHP commands in between the curly brackets. We can also include other IF statments (nested statements).

Other Comparisons

There are some other ways you can use your IF statement to compare the values. Firstly, you can compare two different variables to see if their values match e.g.

if ($enteredpass == $password)

You can also use the standard comparision symbols to check to see if one variable is greater than or less than another:

if ($age < "13")

Or :

if ($date > $finished)

You may also check for the multiple tests in one IF statement formate. For instance, if you have a form and you want to check its any of the fields were left blank you could use:

if ($name == "" || $email == "" || $password == "") {
echo "Please fill in all the fields";
}

News
this is the news
this is the news this is the news this is the news....
openWYSIWYG 1.01 bet....
small description
Links