Free Web Tutorials Free Online Tutorials About Us Contact Us

PHP IF / Else Tutorial:


When PHP evaluates your If...elseif...else statement it will first see if the If statement is true, if it does not evaluate to true it then proceeds to check the next condition which is the elseif and so on.

Here is the basic format of If/Else statements in PHP

Example:

<?php

$firstName = "JOHN";

//evaluate a string
if($firstname == "fred"){
	echo " firstname is fred";
}elseif($firstname == "JOHN"){
	echo " firstname is JOHN.";
}else{
	echo " firstname is fergal.";
}

?>


You can see clearly that

if($firstname == "fred") - evaluate to FALSE JOHN is not equal to fred.

and

elseif($firstname == "JOHN") evaluates to TRUE as JOHN == JOHN.
Thus our output looks like firstname is JOHN. You can multiple elseif statements, however you may consider using LOOPs or other conditionaly checking statements in this case.

php if else tutorial