ECMS V3

This page is brought to you by ECMS V3 - Next Generation Web Management (http://www.evaria.com/). When printing this page (http://www.evaria.com/v3/index/article/35),
you must include the entire legal notice at bottom.

59
district-librarian

If, Elseif and Else syntax

Similar to Switch syntax, maybe the most comprehensive for newbees. A bit slower but "safe"...

4 Published 15.08.2006 - Last edited 15.08.2006 - 6241 views - 0 comments

If syntax:

<?php
If (condition)
{
	# some code
}
else
{
	# other code
}
?>

If "condition" is true some code is run, if not (else) other code is run.

Example:

<?php
$condition1 = 1;
$condition2 = 0;

If ($condition1)
{
echo "<p>condition 1 is true</p>";
}
else
{
echo "<p>condition 1 is false</p>";
}

If ($condition2)
{
	echo "<p>condition 2 is true</p>";
}
else
{
	echo "<p>condition 2 is false</p>";
}
?>

The outcome of the examples are:

condition 1 is true (1 = true)
condition 2 is false (0 = false)

Elseif syntax:

<?php
If ($condition1)
{
	# run code 1
}
elseif($condition2)
{
	# run code 2
}
elseif($condition3)
{
	# run code 3
}
else
{
	# run code 4
}
?>

The difference compared to the If syntax is that you can check for more conditions (similar to Switch syntax).

Eksempel:

<?php
$condition1 = 0;
$condition2 = 1;
$condition3 = 2;

If ($condition1)
{
	echo "<p>condition 1 er sann</p>";
}
elseif ($condition2)
{
	echo "<p>condition 2 er sann</p>";
}
elseif ($condition3)
{
	echo "<p>condition 3 er sann</p>";
}
else
{
	echo "<p>Kunne ikke finne noen av conditionene</p>";
}
?>

Even if $condition3 also is true, the script jumps out of the Elseif at $condition2 as this is the first true condition.


Name:

Email: hidden

Website:

Comment:

Please enter these letters here (Case sensitive) *

While we are updating content in the new site, the original Evaria site can be accessed via this link.

Page generated in 0.050s

evaria.com © 1999-2012 HTML CSS Contact us Back to top