PHP: Simple Strings, Sequences and Errors
Derek Bridge
Department of Computer Science,
University College Cork
PHP: Simple Strings, Sequences and Errors
Aims:
- to learn more about string literals
- to learn about sequences of statements
- to learn about compiling
- to learn about types of error
Strings
Examples of strings:
'Hello world!' | 'CS1109' |
'<strong>Hello world!</strong>' | '1109' |
'Homer says "Doh!" a lot' | '<section id="intro">' |
'' | ' ' |
Class exercise Are the last two examples the same?
(Hint: what length are they? Hint: only one of them is called the
empty string)
Escape sequences in strings
- Class exercise: Why does this cause problems:
'New Year's resolutions'
- Escape sequences when using single quotes for strings:
- to include an apostrophe, use \'
- to include a backslash, just use \, but also \\
- Example:
'New Year\'s resolutions'
Two examples of long strings
'In many programming languages, a string must start and finish all one line'
'But, in PHP, if you need to use a long string,
you can split it
over several lines
if you want to,
without getting any error messages'
My resolutions: Version 1, resolutions1.php
This version uses one statement to echo one long string
<body>
<?php
echo '<p>These are my New Year\'s resolutions:</p>
<ul id="resolutions">
<li>less smiling;</li>
<li>more swearing.</li>
</ul>';
?>
</body>
My resolutions: Version 2, resolutions2.php
This version uses a sequence of statements, which are executed
one after the other. Notice the semi-colons to separate the statements
<body>
<?php
echo '<p>These are my New Year\'s resolutions:</p>';
echo '<ul id="resolutions">';
echo '<li>less smiling;</li>';
echo '<li>more swearing.</li>';
echo '</ul>';
?>
</body>
Revision
Remind yourself what happens when a client requests resolutions2.php
(copy mode and interpret mode)
More detail
- What happen when the user generates an HTTP request to a PHP 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 translates any snippets of PHP to
instructions in a lower-level
language, closer to the one that the computer actually understands
- The PHP processor works through the new version of the file
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
More detail
Some terminology
- This 'translation',
- from source language (in our case PHP)
- to target (or object) language (a lower-level language,
closer to the one that the computer actually understands)
is called compilation
Types of error
- Compilation errors
- discovered by the PHP processor during compilation, resulting in an
error message
- Runtime errors
- discovered by the PHP processor during execution, resulting in an
error message or a warning message
- Logic errors
- the output is not as you wanted/expected
- it's your job to spot these; there's no error/warning message
Compilation errors
- Syntax errors
- Where you've not followed the rules concerning the format of the PHP
statements
- Classic examples: missing semi-colons; missing end quotes; missing
end PHP tag
- These errors are always fatal:
- You get an error message
- Compilation has failed and the script is not executed
- For some programming languages (but not PHP), some other errors can be detected
during compilation
Runtime errors
- Where your program tries to do something that probably makes no sense
- Some of these errors are fatal (execution is aborted) and produce error messages
- Classic PHP example: calling an undefined function
- Others are non-fatal and simply produce a warning message or no message
at all
- Classic PHP example: division by zero
- Some other programming languages are more severe about these errors
Class exercises
- What output will result:
echo '1\2 UCC\'s lecturers are *\\?\n"%!';
- What output will result:
echo '1\2 UCC's lecturers are *\\?\n"%!';