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/34/switch-syntax.html),
you must include the entire legal notice at bottom.

33
district-librarian

Switch syntax

If and Switch are very similar, but Switch is "prettier" and faster.

5 Published 15.08.2006 - Last edited 15.08.2006 - 10063 views - 0 comments

<?php 
switch (variable)
{
	case ´value1´:
	# put code here (do this)
	break;
	
	case ´value2´:
	# put code here (do that)
	break;
	
	default:
	# put code here (do default)
	break;
}
?>

Example:

<a href="index.php?page=1">Open page 1</a>
<a href="index.php?page=2">Open page 2</a>
<a href="index.php?page=3">Open page 3</a>

<?php
# The variable is page (see the links above)
# The values are given through the link (string)
switch (0)
{
	case ´1´:
	include ("1.inc.php");
	break;
	
	case ´2´:
	include ("2.inc.php");
	break;
	
	case ´3´:
	include ("3.inc.php");
	break;
	
	default:
	include ("error.inc.php");
	break;
}
?>

When clicking a link the selected value are handed to the script and if found it executes your case or default if not found. You can use this for page navigation shown in this example or any other situation where you have multiple choices depending on one variable. It is also possible to include a second Switch inside any of the cases above.


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.048s

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