Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

The if Decision Construct

If is the most common control statement. It tests to see if a statement is true or false. If the statement is true, PHP executes a specified statement block.

The syntax is as follows:

if(test expression){
	statement block line1
	statement block line 2
	statement block line3...
}

The statement block is going to be executed is the statement enclosed in parenthesis evaluates to true. Let's take an example:

if($age==18){
	print "you are a young adult");
}

In this code the line "you are a young adult" will only be printed if the age was supplied as 18. If you had assigned a different value to the variable $age then this will not be printed.

Remember there's a difference between = and ==. == is used to test equality (it's the one you'll use in an if test expression). Most amateur programmers always put =. If you do, the line of code above will be printed at all times irrespective. This is because if you remember from lesson 1, = is used to assign NOT to test.

<< Decision-Making Control Structures | The if... else Decision Construct >>

JamHitz Productions