This site will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device.


 

Dodo's Scripts Collection


Basic TutorialsBasic Tutorials

Write your own function
To improve the switch function we previously learned, you may write your own function. First, add
<?php
function self_include() {
	global $_SERVER;
	$z = basename($_SERVER['QUERY_STRING']);
	// make php extension for it
	$z .= ".php";
	include($z);
}
?>
in front of:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Then modify the original code to:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>

<?php
	switch($_SERVER['QUERY_STRING']) {
		case "love":
			self_include();
			break;
		case "nolove":
			self_include();
			break;
		// the following part allows to you call your file without "?"
		case "";
			include("home.php"); // or whatever you want. make sure it exists!
			break;
		default:
			echo "Sorry, the page doesn't exist!";
	}
?>

</BODY>
</HTML>
mod2_test4.php?love and mod2_test4.php?nolove.

Does that save a lot of coding? Unfortunately not too much. This means if you do have 50 files, you are better off with regular inclusion or dynamic inclusion. So what is there a way to gracefully handle errors with those two? Not really for regular inclusion but for dynamic inclusion, yes, there is :).
Back