PHP: Introduction

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Introduction

Aims:

Static vs. dynamic web pages

PHP

Programming languages and scripting languages

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