PHP: Introduction
Derek Bridge
Department of Computer Science,
University College Cork
PHP: Introduction
Aims:
- to know the difference between static and dynamic web pages
- to be able to write our first few PHP scripts
- to understand how the server and the PHP processor handle PHP scripts
Static vs. dynamic web pages
- Static web pages
- (X)HTML/CSS stored in files on server
- the only way to change them is to edit the files
- Dynamic web pages
- programs stored on the server are executed and they output (X)HTML/CSS
- the output may depend on the date, user input, contents of a database,...
- Example: staff list on Departmental web site
- Example: most of the Departmental web site!
PHP
- PHP originally
- "Personal Home Page" Tools
- Developed in 1994 by Rasmus Lerdorf
- For logging accesses to his web site
- PHP now (Version 5)
- "PHP: Hypertext Preprocessor"
- Developed and used by a large community (> 2.5 million people)
- A fully-fledged programming language but mostly used for server-side scripting
- Most widely-used programming language on the web (for >40% of all webapps):
http://www.php.net/usage.php
Programming languages and scripting languages
- PHP is sometimes called a scripting language
- A scripting language is a special kind of programming language
- A program written in a scripting language is sometimes called a script
- So where's the difference?
- A script usually drives some other system, e.g.:
- A client-side script gets the browser to do things
- A server-side script gets the server to do things
- A shell script gets the operating system to do things
- Scripts usually do this by invoking pre-existing components (which may
not be written in the scripting language)
- Scripts are often interpreted (e.g. by a module in the browser,
server or operating system)
greetings1.php: our first PHP script
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Our first PHP script</title>
</head>
<body>
<p>
<?php
echo 'Happy New Year!';
?>
</p>
</body>
</html>
Notes on greetings1.php
- Filename extension is
.php
, not .html
- Embed the PHP code between
<?php
and ?>
- Alternatives:
<? ... ?>
<% ... %>
<script> ... </script>
- Use whitespace (spaces and new lines) for readable layout
- Keywords are case-insensitive:
echo, ECHO, Echo,...
- Semicolons separate simple statements
Notes on greetings1.php
- The
echo
command puts a string into the (X)HTML
- Other ways to put strings and other values into the (X)HTML:
print
printf
print_r
(also var_dump
)
- Strings can be written using
- single quotes, e.g. 'Happy New Year!'
- double quotes, e.g. "Happy New Year!"
- heredoc style (which we'll ignore)
But there is a difference!
Notes on greetings1.php
- What happen when the user generates an HTTP request to this file
(by typing the URL or by clicking on a link)?
- Because of the file extension (
.php
), the server passes the
request to the PHP processor
- The PHP processor works through the script line by line and handles each line
using either
- copy mode, or
- interpret mode (execute)
- The PHP processor's output is sent to the client browser
Notes on greetings1.php
Distinguish:
- The PHP script
- a mix of (X)HTML and snippets of PHP
- since it's not just (X)HTML, probably will not validate
- The output of the PHP processor:
- just (X)HTML, no remaining PHP
- since it's just (X)HTML, this can validate
- (Note in general that a PHP script can produce other resources too,
e.g. CSS stylesheets, JavaScript, PDFs, GIFs, JPEGs, PNGs, ...)
greetings2.php
: our second script, which outputs tags
...
<body>
<?php
echo '<p>Many happy returns!</p>';
?>
</body>
</html>
greetings3.php
: our third script
<body>
<?php
echo '<p>These are my New Year\'s resolutions:</p>';
echo '<ul div="resolutions">';
echo '<li>less smiling;</li>';
echo '<li>more swearing.</li>';
echo '</ul>';
?>
</body>
</html>
Notes on greetings3.php
- Escape sequences when using single quotes for strings:
- to include an apostrophe, use \'
- to include a backslash, just use \, but also \\
- Class exercises
- What output will result from the following:
echo '1\2 UCC\'s lecturers are *\\?\n"%!';
echo '1\2 UCC's lecturers are *\\?\n"%!';
- Strings in PHP can be written using single or double quotes.
What was the advantage of using single quotes in
greetings3.php
?
A problem
In fact, on some installations (including ours) these PHP scripts will
generate errors, due to their first line. Why?
Solution: Either change the way PHP is configured, or change the first
line (as we shall do)...
E.g. greetings1.php
becomes:
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Our first PHP script</title>
</head>
<body>
<p>
<?php
echo 'Happy New Year!';
?>
</p>
</body>
</html>
info.php
: this script runs a PHP function
<body>
<?php
phpinfo();
?>
</body>
</html>
Alternative technologies: PHP is not the only option
- Server modules
- the PHP processor
- also Perl, Server-Side JavaScript (SSJS), Active Server Pages (ASP),...
- Common Gateway Interface (CGI)
- a CGI program is a 'normal' program
- written in any language (C, Perl, Python,... are common)
- stored in a special directory, known to the server (e.g.
/cgi-bin
)
- the server sends input data to the program (CGI specifies the format)
- the program outputs (X)HTML that is sent to the client browser
- Other, e.g.
- Java Servlets and Java Server Pages (JSP)
- ASP.NET