PHP: Simple Strings, Sequences and Errors

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Simple Strings, Sequences and Errors

Aims:

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

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

More detail

The client requests a dynamic Web page. The server retrieves a program from disk. It translates the programs from PHP to a lower-level language. This is compilation. Then it executes the new version of the program - the one in the lower level language. This is interpretation.

Some terminology

Types of error

Compilation errors

Runtime errors

Logic errors

Class exercises