User Tools

Site Tools


eg-259:lecture16

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
eg-259:lecture16 [2011/03/25 12:06] eechriseg-259:lecture16 [2013/03/08 18:03] (current) – [Introduction to PHP (Part 1)] eechris
Line 2: Line 2:
 ====== Introduction to PHP (Part 1) ====== ====== Introduction to PHP (Part 1) ======
  
-**Lecture 16**: To be given on Tuesday 29th March, 2011.+**Supplementary Material** 
 + 
 +Provided for Reference. This material is no longer taught on this module.
  
 **Lecturer**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]]. **Lecturer**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]].
Line 47: Line 49:
 //At the end of this lecture you should be able to answer these questions//: //At the end of this lecture you should be able to answer these questions//:
  
-  - How Does Web server determine whether a requested document includes PHP code +  - How does web server determine whether a requested document includes PHP code 
   - What are the two modes of the PHP processor?    - What are the two modes of the PHP processor? 
   - What are the syntax and semantics of the include construct?    - What are the syntax and semantics of the include construct? 
Line 99: Line 101:
   * PHP is a server-side scripting language whose scripts are embedded in HTML documents    * PHP is a server-side scripting language whose scripts are embedded in HTML documents 
   * PHP is an alternative to CGI, ASP.NET, JSP, and Allaire's ColdFusion    * PHP is an alternative to CGI, ASP.NET, JSP, and Allaire's ColdFusion 
-  * The PHP processor has two modes: copy (XHTML) and interpret (PHP) +  * The PHP processor has two modes: copy (HTML) and interpret (PHP) 
   * PHP syntax is similar to that of JavaScript    * PHP syntax is similar to that of JavaScript 
   * PHP is dynamically typed    * PHP is dynamically typed 
Line 107: Line 109:
 ===== General Syntactic Characteristics ===== ===== General Syntactic Characteristics =====
  
-  * PHP code can be specified in an XHTML document internally or externally:+  * PHP code can be specified in an HTML document internally or externally:
     * Internally: ''<?php … ?>''      * Internally: ''<?php … ?>'' 
     * Externally: ''include ("myScript.inc")''      * Externally: ''include ("myScript.inc")'' 
-  * The file can contain both PHP and XHTML +  * The file can contain both PHP and HTML 
   * If the file contains PHP, the PHP must be enclosed in tags ''<?php … ?>'', even if the include statement is already in ''<?php … ?>''    * If the file contains PHP, the PHP must be enclosed in tags ''<?php … ?>'', even if the include statement is already in ''<?php … ?>'' 
   * Every variable name begins with a ''$''    * Every variable name begins with a ''$'' 
Line 180: Line 182:
  
 <note> <note>
-**Characters are single bytes**: This means that, unlike XHTML, PHP does not natively support Unicode characters. There are a pair of functions ''utf8_encode()'' and ''utf8_decode()'' which provide some Unicode support. There are implications of this if you want to develop PHP applications accessible in countries which do not use the //utf-8// character set.+**Characters are single bytes**: This means that, unlike HTML, PHP does not natively support Unicode characters. There are a pair of functions ''utf8_encode()'' and ''utf8_decode()'' which provide some Unicode support. There are implications of this if you want to develop PHP applications accessible in countries which do not use the //utf-8// character set.
 </note> </note>
  
Line 246: Line 248:
 ---- ----
   * This shorthand should only be used for printing variables or with functions that return a string.   * This shorthand should only be used for printing variables or with functions that return a string.
-  * The short syntax only works with the [[http://uk2.php.net/manual/en/ini.core.php#ini.short-open-tag|short_open_tag]] configuration setting enabled (which it is in XAMPP). +  * The short syntax only works with the [[http://uk2.php.net/manual/en/ini.core.php#ini.short-open-tag|short_open_tag]] configuration setting enabled (which it is in Ubuntu). 
  
 ===== Hello World (PHP) ===== ===== Hello World (PHP) =====
Line 252: Line 254:
   * PHP code is placed in the body of an HTML document   * PHP code is placed in the body of an HTML document
 <code html> <code html>
-    <html> +<!DOCTYPE html> 
-      <head> +<html lang="en"> 
-        <title> Trivial php example </title> +  <head> 
-      </head> +    <meta charset="utf-8" /> 
-      <body> +    <title> Trivial php example </title> 
-        <?php +  </head> 
-           print "Welcome to my Web site!"; +  <body> 
-        ?> +    <?php 
-      </body> +      print "Welcome to my Web site!"; 
-    </html> +    ?> 
 +  </body> 
 +</html> 
 </code> </code>
  
Line 286: Line 290:
  
 <code php> <code php>
-<?php echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); ?>  +<!DOCTYPE html>
-<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1 //EN" +
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">+
  
 <!-- today.php - A simple example to illustrate a PHP document --> <!-- today.php - A simple example to illustrate a PHP document -->
-<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"> +<html lang="en"> 
-  <head> <title> today.php </title>+  <head>  
 +    <meta charset="utf-8" /> 
 +    <title> today.php &ndash; A simple example to illustrate a PHP document  </title>
   </head>   </head>
   <body>   <body>
Line 304: Line 308:
     </p>     </p>
   </body>   </body>
-</html> +</html>
 </code> </code>
  
Line 311: Line 315:
 {{eg-259:l16-today.png|Result of executing PHP script today.php}} {{eg-259:l16-today.png|Result of executing PHP script today.php}}
  
-<note warning> 
-The first line of the code 
-<code php> 
-<?php echo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?>  
-</code> 
-Is necessary to protect the XML declaration (needed for valid XHTML) 
-<code html> 
-<?xml version="1.0" encoding="utf-8"?> 
-</code> 
-from the PHP parser. The code simply asks PHP to //echo// the declaration rather than ecoding it directly in the XHTML part of the script. If you do not do this you get this mysterious error message! 
- 
-**Parse error**: syntax error, unexpected T_STRING in **C:\xampp\htdocs\eg-259\examples\lecture16\today.php** on line 1 
-</note> 
    
  
Line 379: Line 370:
   * Code:   * Code:
 <code php> <code php>
-<?php echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); ?>  +<!DOCTYPE html>
-<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1 //EN" +
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">+
  
 <!-- powers.php <!-- powers.php
      An example to illustrate loops and arithmetic      An example to illustrate loops and arithmetic
      -->      -->
-<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"> +<html lang="en"> 
-  <head> <title> powers.php </title>+  <head>  
 +    <meta charset="utf-8"> 
 +    <title> powers.php &ndash An example to illustrate loops and arithmetic </title>
   </head>   </head>
   <body>   <body>
Line 412: Line 403:
     </table>     </table>
   </body>   </body>
-</html> +</html> 
 </code> </code>
  
Line 444: Line 436:
 ---- ----
  
-<note>+<note warning>
 this is not necessarily a good idea! Notice how the end brace for the conditional is separated from the loop body by several unrelated HTML tags. This produces fragile code -- if the second set of PHP tags where removed, a syntax error would occur which might be difficult to track down. this is not necessarily a good idea! Notice how the end brace for the conditional is separated from the loop body by several unrelated HTML tags. This produces fragile code -- if the second set of PHP tags where removed, a syntax error would occur which might be difficult to track down.
-</code>+</note>
  
  
eg-259/lecture16.1301054764.txt.gz · Last modified: 2011/03/25 12:06 by eechris