Chapter 5 Answers

Question 5-1

Using functions avoids the need to copy or rewrite similar code sections many times over by combining sets of statements together so that they can be called by a simple name.

Question 5-2

By default, a function can return a single value. But by utilizing arrays, references, and global variables, any number of values can be returned.

Question 5-3

When you reference a variable by name, such as by assigning its value to another variable or by passing its value to a function, its value is copied. The original does not change when the copy is changed. But if you reference a variable, only a pointer (or reference) to its value is used, so that a single value is referenced by more than one name. Changing the value of the reference will change the original as well.

Question 5-4

Scope refers to which parts of a program can access a variable. For example, a variable of global scope can be accessed by all parts of a PHP program.

Question 5-5

To incorporate one file within another, you can use the include or require directives, or their safer variants, include_once and require_once.

Question 5-6

A function is a set of statements referenced by a name that can receive and return values. An object may contain zero or many functions (which are then called methods) as well as variables (which are called properties) all combined in a single unit.

Question 5-7

To create a new object in PHP, use the new keyword like this: $object = new Class.

Question 5-8

To create a subclass, use the ...

Get Learning PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.