Tuesday, May 26, 2009

PHP Tutorial conditional statement: if statement

The if statement

The if statement is a way of controlling the execution of a statement that follows it. The if statement evaluates an expression between parentheses. If this expression results in a true value, the statement is executed. Otherwise, the statement is skipped entirely.

The if statement structure:

if ( expression ) {
// code to execute if the expression evaluates to true
}

Example of using the if statement:



You use the comparison operator >= to compare the variable $grade with the number 83. If the number is greater than or equal to 83, the expression evaluates to true, and the code block below the if statement is executed.

Put these lines into a text file called gradeif.php, and place this file in your Web server document root. When you access this script through your Web browser, it produces the following:

Congratulations!, you pass the subject

If you change the value of $grade to to numbers that is less than 75 and run the script, the expression in the if statement evaluates to false, and the code block is skipped. It will not display anything to your browser.

Related Post:

Tutorial HTML heading

HTML (Hypertext Markup Labguage) Structure

Hypertxt Markup Language HTML