In some other languages a do…while loop is called an 'until' loop. In most cases it makes little difference whether a while or a do … while loop is used, but it may sometimes be signifi cant whether or not the loop should be executed at least once.
3.9 The Include and Require Statements
These two statements are very similar and can often be used to very valuable effect.
The PHP statement
tells the PHP system to 'include and evaluate' the contents of an external fi le "fi le1.
php".
To illustrate what this means we will start with an example where fi le1.php con-tains only PHP statements. Suppose that fi le1.php concon-tains
The following script
is equivalent to a script containing
49
The statements in an included 'PHP only' fi le will often be defi nitions of one or more 'user-defi ned functions', which will be discussed in a later Chapter. Another possibility is that the fi le may contain frequently used statements, e.g. to produce standard headings and/or a standard menu at the top of all of a company's web pages. Another example relating to accessing a MySQL database is given in Chap. 15 .
The situation is more complicated when an included fi le contains HTML (out-side PHP tags of course) as well as (possibly) one or more PHP scripting blocks. In this case any HTML will be passed directly to the web browser to interpret. To illustrate this, suppose that fi le2.php contains
The following script
will produce the output
The sequence of events when fi le fi le2.php is 'included' is as follows:
• The two statements in the fi rst PHP scripting block are executed. Variable
$forename is given the value "John" and the string abcde < br > is sent to the web browser and displayed as abcde followed by a newline character.
• Next the two lines of HTML after the closing PHP tag are 'evaluated', i.e. they are passed to the web browser and displayed as the two lines
3.9 The Include and Require Statements
with each line of text followed by a paragraph break.
• Next the two statements in the second PHP scripting block are executed. Variable
$surname is set to "Smith" and the string fghij < br > is sent to the web browser and displayed as fghij followed by a newline character.
• As fi le fi le2.php has now been fully 'included and evaluated' the original PHP script continues execution. The words 'My name is John Smith' are sent to the web browser and displayed.
Although an included PHP fi le generally contains only PHP or a mixture of PHP and HTML, it is also possible for it to contain no PHP at all, just HTML. This can be used to valuable effect.
The HTML source text of every webpage begins in a standardised way, similar to:
and ends in a standardised way, i.e.
To save the bother of typing this every time we can put the former into a fi le start.
php and the latter into a fi le end.php (both without PHP tags).
Then if we wish to use a PHP script to create a complete webpage we can include the two fi les in our PHP like this.
The sixth line of start.php refers to a style sheet fi le mystyle.css. If the approach shown above is used to generate a large number of webpages and later it is decided to use a different style sheet, all that has to be altered is one line in fi le start.php, not one line in each of many fi les.
51
The require statement is identical to the include statement with one difference. If the specifi ed fi le cannot be loaded or does not exist an include statement will gener-ate a warning message but the script will continue. The require stgener-atement will termi-nate execution. Although the latter seems generally preferable, in practice we are most unlikely to try to include a non-existent fi le and for all practical purposes we can use include and require interchangeably.
There are two other variants: the include_once and require_once statements. If these statements are used and for any reason the contents of the fi le specifi ed have already been 'included and evaluated' it will not be loaded again. These forms of the statement are unlikely to be of more than very marginal benefi t in practical applications.
Chapter Summary
This chapter describes seven of the eight principal types of statement in PHP and their variants: assignment, Print, If, Switch, For, While/Do…While and Include/Require/Include_once/Require_once. (The eighth statement, Foreach, will be described in Chap. 4 .) Rules for evaluating complex arithmetic, string and logical expressions are described in detail and a simplifi ed notation for some of the most common types of assignment is described. The signifi cant role of the Print statement and alternative ways in which it may be used in generating the output displayed in the user's web browser are explained. The chapter goes on to explain the If statement and the closely related Switch statement. Three types of looping statement: For, While and Do…While are then described. The chapter concludes with a description of the Include state-ment and its variants. to output the position of $val in array $numbers or a statement that it is not in the array. Do this (a) using a While loop and (b) using a Do…While loop.
(6) Write an If statement that takes a variable $month containing the name of a month, e.g. "October", and outputs the number of days it contains.
3.9 The Include and Require Statements
53
© Springer International Publishing Switzerland 2015 M. Bramer, Web Programming with PHP and MySQL, DOI 10.1007/978-3-319-22659-0_4
More About Arrays
Chapter Aims
After reading this chapter you should be able to:
• use arrays when writing PHP scripts, especially in conjunction with the Foreach statement
• use the various sort functions available in PHP and understand the differ-ences between them
• distinguish between indexed arrays and associative arrays
• use the explode and implode functions to convert strings with internal sep-arator characters, such as commas, into arrays and vice versa.
Arrays were introduced in Chap. 2 as a form of variable in which many values, known as array elements, can be referred to using the same name, distinguished by a numerical index contained in square brackets. There is much more to arrays than this, as we will illustrate in this chapter.