Difference between revisions of "Quick Reference for PHP"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
(Switch is the Equivalent of Case)
 
(5 intermediate revisions by one user not shown)
Line 3: Line 3:
 
See also: [[PHP Function Reference]]
 
See also: [[PHP Function Reference]]
  
<?
+
  <?
  header('Location: http://members.robotz.com/');
+
    header('Location: http://members.robotz.com/');
?>
+
  ?>
  
 +
== Read Query String ==
  
 +
  <?
 +
    <nowiki>$qString = $_SERVER['QUERY_STRING'];</nowiki>
 +
    <nowiki>$qString = preg_replace('#[^a-zA-Z0-9_-]#', '', $qString);</nowiki>
 +
    <nowiki>print "QUERYSTRING= $qString<BR>";</nowiki>
 +
    <nowiki>if ($qString == "") $qString = "home";</nowiki>
 +
    if ($qString == "home") {
 +
      <nowiki>?><i>Viewing the HOME PAGE</i></a> <?</nowiki>
 +
    } elseif ($qString == "other") {
 +
      <nowiki>?><i>Viewing some other page</i></a> <?</nowiki>
 +
    }
  
 +
The line with preg_replace protects against known query string exploits.  The null assignment could be done better, however, the example is for syntax purposes.
 +
 +
== Switch is the Equivalent of Case ==
 +
 +
<nowiki><?</nowiki>
 +
<nowiki>switch ($i) {</nowiki>
 +
<nowiki>    case 0:</nowiki>
 +
<nowiki>        echo "i equals 0";</nowiki>
 +
<nowiki>        break;</nowiki>
 +
<nowiki>    case 1:</nowiki>
 +
<nowiki>        echo "i equals 1";</nowiki>
 +
<nowiki>        break;</nowiki>
 +
<nowiki>    case 2:</nowiki>
 +
<nowiki>        echo "i equals 2";</nowiki>
 +
<nowiki>        break;</nowiki>
 +
<nowiki>    default:</nowiki>
 +
<nowiki>      echo "i is not equal to 0, 1 or 2";</nowiki>
 +
<nowiki>}</nowiki>
 +
<nowiki>?></nowiki>
 +
 +
Or with string variables
 +
 +
case "apple":
 +
 +
&nbsp;
  
 
[[Category:Computer Technology]]
 
[[Category:Computer Technology]]
 
[[Category:Programming]]
 
[[Category:Programming]]
 
[[Category:PHP]]
 
[[Category:PHP]]

Latest revision as of 15:48, 18 April 2014

Redirect with PHP

See also: PHP Function Reference

 <?
   header('Location: http://members.robotz.com/');
 ?>

Read Query String

 <?
   $qString = $_SERVER['QUERY_STRING'];
   $qString = preg_replace('#[^a-zA-Z0-9_-]#', '', $qString);
   print "QUERYSTRING= $qString<BR>";
   if ($qString == "") $qString = "home";
   if ($qString == "home") {
     ?><i>Viewing the HOME PAGE</i></a> <?
   } elseif ($qString == "other") {
     ?><i>Viewing some other page</i></a> <?
   }

The line with preg_replace protects against known query string exploits. The null assignment could be done better, however, the example is for syntax purposes.

Switch is the Equivalent of Case

<?
switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
       echo "i is not equal to 0, 1 or 2";
}
?>

Or with string variables

case "apple":