Server-Side PHP Programming
An Introduction with Examples |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
.php
extension.php
file (i.e., most
HTML elements, CSS) are just inserted into the HTTP response
.php
file (see below) are
executed and the output is inserted into the HTTP
response<?php
and
?>
(XML/XHTML style)script
element
(HTML style)<?
and
?>
<%
and
%>
(ASP style)echo
Statement:
echo "<p>Hello world!</p>"; echo "<p>Hello $name.</p>"; // $name is evaluated echo "<p>Hello find($name).</p>"; // find() is not evaluated but $name is evaluated echo $name; echo '<p>Hello $name.</p>'; // $name is not evaluated
print
:
echo
(i.e.,
without parentheses)printf()
:
Loading the PHP Classes/Functions
<script language="PHP"> require_once('scripts/codeProcessor.phps'); </script>
Insertion of the Code Fragment
<pre> <script language="PHP"> $code = file('javaexamples/datahiding/Node.java'); processCode($code, ''); // Uses echo and/or printf </script> </pre>
<?php function createDirectoryListing() { $dir = opendir("."); while (($file = readdir($dir)) !== false) { if ((strpos($file, ".php")) !== false) { $text = ""; $code = file($file); for ($i=0; $i<count($code); $i++) { $text = $text . $code[$i]; } $start = strpos($text, "<title>"); $end = strpos($text, "</title>"); if (($start !== false) && ($end !== false)) { $length = $end - $start - 7; $title = substr($text, $start+7, $length); if ($length > 0) { $item[$file] = $title; } } } } asort($item, SORT_STRING); foreach ($item as $file => $title) { echo "<ul>"; echo "<a href=\"" . $file . "\">" . $title . "</a>"; echo "<br />"; echo "</ul>"; } } ?>
$_SERVER
Array:
$_SERVER
Array:
["REQUEST_METHOD"]
- either "GET"
or
"POST"
["SERVER_PROTOCOL"]
["SERVER_NAME"]
["SERVER_PORT"]
["PATH_INFO"]
and ["PATH_TRANSLATED"]
["QUERY_STRING"]
["CONTENT_TYPE"]
["CONTENT_LENGTH"]
["REMOTE_HOST"]
and ["REMOTE_ADDR"]
$_GET
Array:
$_POST
Array:
Content-Type
of
application/x-www-form-urlencoded
or
multipart/form-data
)$_COOKIE
Array:
setcookie()
Function:
header()
Function:
Content-Length
)is_null()
returns true
if the
parameter is
NULL
isset()
returns true
if the parameter
is set and not NULL
is_array()
, is_bool()
,
is_float()
, is_int()
,
is_object()
, and is_string()
boolval()
, floatval()
, and
intval()
htmlentities()
urlencode()
/urldecode()
and
rawurlencode()
/rawurldecode()
parse_url()
filter_var()