• No results found

Default Values for Arguments

In document Web Programming With PHP and MySQL (Page 134-140)

The output from the script confi rms that all the elements of $x have been set to zero.

8.8 Default Values for Arguments

Another unusual feature of functions in PHP is the facility to omit some of the argu-ments when calling a function, in which case specifi ed default values are used.

The example below shows a function with four arguments, all passed by value.

The third and fourth arguments both have specifi ed default values.

The output from the three function calls is shown below. It can be seen that if the third and fourth arguments are omitted they are treated as having default values, namely 120 in the case of $c and the string "dog" in the case of $d.

When a variable is passed by value the default value must be a constant, not a variable or a function call, etc. As the next example shows, the variable can also be an array of constants.

125

The output from this script is

Note that when using default arguments in a function defi nition, any such argu-ments should be specifi ed to the right of any non-default arguargu-ments. When the func-tion is called any omitted values must be the right-most ones in the argument list. So in the case of test2 above, the function call test2(60,90,array(12,24,−6)) would be valid but test2(60,90,,"cat") would not be.

From PHP version 5 onwards it has been possible for variables passed by refer-ence to have default values too, but that possibility will not be discussed here.

Practical Exercise 8

(1) Defi ne a function hypo that takes the length of the two shorter sides of a right- angled triangle as arguments and returns the length of the hypotenuse.

(2) Defi ne a function that prints out the contents of a two-dimensional array row-by-row.

(3) Convert your answer to (2) to print out the contents in the form of a table.

(4) Defi ne a function that displays a specifi ed image, with a specifi ed width and height. The default values of width and height should be 150 and 200 respectively.

(5) Defi ne a function that sets all the elements of a two-dimensional array to a given value (default zero).

Chapter Summary

This chapter demonstrates how users can defi ne their own functions as part of a PHP script. The concept of global and local variables is introduced and the value of creating a function library is discussed. The chapter goes on to con-sider related issues such as passing an array as an argument of a function and the difference between passing arguments by value and by reference. Finally it is shown how to give arguments default values

8.8 Default Values for Arguments

127

© Springer International Publishing Switzerland 2015 M. Bramer, Web Programming with PHP and MySQL, DOI 10.1007/978-3-319-22659-0_9

Passing Variables to a PHP Script I

Chapter Aims

After reading this chapter you should be able to:

• understand in detail the components of a web form

• write a webform of your own using a combination of HTML and PHP function calls

9.1 Introduction

In this chapter we look at the most common way of passing the values of variables into a PHP script: the use of a webform, normally written in HTML, to send values to a PHP script which we will call a destination page .

Although this book is not about HTML, the topic of webforms is a major excep-tion. Unless you have previously used PHP (or some similar language) to write destination pages it is most unlikely that you have used HTML to write webforms and we will assume that you have not. We will see that even for quite basic web-forms it can save a great deal of effort and avoid a lot of errors to use PHP to gener-ate some or all of the lines of HTML.

In the next chapter we will discuss how to write the PHP statements for a destina-tion page to make use of the values passed from a webform and will discuss two other ways in which values can be passed into a PHP script.

128

9.2 Webforms

Many organisations now invite their users, customers etc. to provide information by fi lling in a form on the screen and pressing a 'submit' button. This is a typical (but simple) example of a webform .

Filling in the form and pressing Submit will send the information to another webpage, which we will call the destination page , and the user's web browser will automatically move to that page. The destination page must be written in PHP or some similar language (not plain HTML). On receiving the values passed from the 'sending' page the destination page will take some action, create or update a data-base record or some combination of such actions. We will come on to destination pages in Chap. 10 .

This simple example illustrates seven different types of box, button etc., known collectively as form objects .

• The one-line horizontal boxes for Forename and Surname are known as text boxes or text fi elds . These are each 20 characters wide but up to 50 characters may be typed in each. (When there are more than 20 characters entered, the left-most ones scroll off to the left and become unreadable.) We will see how to specify the 20 and 50 values when we look at the HTML used to generate the form.

• A common convention is to place an asterisk after a form object if it is compul-sory for the user to complete it. Here we are insisting that Forename, Surname and Address are provided. There will need to be a test that these boxes are not empty in the destination page.

9 Passing Variables to a PHP Script I

• The box next to Address is called a textarea . This one is two rows high and 24 columns wide. The user can type any amount of text into the textarea and it will automatically wrap around as the right-hand edge of the box is reached. If more than two lines are typed the uppermost ones will scroll up and become unreadable.

• The line beginning Age Group is an example of the use of radio buttons . The user can select at most one of the options. Selecting an option makes the small circle (the 'button') turn black. Clicking on another option makes the fi rst button clear again and turns the second one black. The complete set of radio buttons is known as a radio group .

• The line beginning Nationality illustrates a 'Select Box'. Clicking on the arrow (or other marker) to the right of the word British will produce a drop-down menu with a (short) list of alternative nationalities.

• The small square to the right of the words 'I agree to the terms and conditions' is a checkbox. Clicking on it once puts a small tick (or similar symbol) into the box indicating that the option is selected. Clicking on the box again removes the contents making the option unselected.

• There needs to be a Submit button at the end of every web form. Pressing it sends all the values entered in the form to the destination page and moves the user's web browser automatically to that page.

• The Reset button can sometimes be useful, especially if the user makes mistakes in entering some of the values. Pressing it returns the web form to its original state.

Although writing a web form can be done entirely in HTML it is a tedious and error-prone task if the form is anything but trivial, and can be made considerably faster by using a PHP script, or perhaps a number of small fragments of PHP to generate the lines of HTML automatically and then output them using PHP print statements.

Apart from the buttons, most form elements can be given default values that are used if the user does not change them. In the above example the only fi eld with a default value is the Select Box labelled Nationality, which has a default of British.

Default values that are always the same for every user can be specifi ed entirely in HTML, as will be explained below. However this is not possible when the default values may vary from one user to another, e.g. name and address values that have been taken from a database. In this case the default value needs to be entered into the form using a PHP script.

The HTML needed to generate the form given above is shown below with the lines numbered for ease of reference.

130

Every web form begins with a <form> tag and ends with a </form> tag (see lines 2 and 24 above). The <form> tag and form elements have a number of attribute=value combinations such as method="post". Some of these are compulsory, others are optional. The value given after the = sign should normally be enclosed in double quotes, unless it is purely numerical and defi nitely must be if it contains any embed-ded spaces. Apart from the right-hand side of an attribute=value pair and any default values, upper and lower case letters can be used interchangeably in a web form.

Some of the less-commonly used attributes are accepted by one web browser, but ignored by another. Naturally you will have no control over the browser a user chooses to use to access your pages, so non-standard attributes are best avoided. We will not attempt to include comprehensive details of every possible attribute here;

just the ones that you are most likely to need to use and that are accepted by most or all common web browsers.

9.2.1 The <form> Tag

• The value of the name attribute is normally unimportant. However, if there is more than one form on the same page they should be given different names.

• The method attribute should normally be set to "post", which indicates that the values are to be sent to the destination page 'invisibly' and retrieved by that page as we shall illustrate in Chap. 10 . Other possibilities will not be considered in this book.

9 Passing Variables to a PHP Script I

• The value of the action attribute is the address of the destination page. This can either be an absolute address beginning with http:// or https:// or an address rela-tive to the sending page. In the case of action="mydir/destin1.php" the destina-tion page is fi le destin1.php in the folder 'mydir', which is a subfolder of the one in which the sending page is located. To indicate the current directory or its par-ent directory we can use the notation . and .. respectively. Thus to indicate that the destination page is the same as the sending page we can put action="."

In document Web Programming With PHP and MySQL (Page 134-140)