Chapter 13

PHP: Functions and HTML Controls

IN THIS CHAPTER

  • Creating functions
  • Passing data to functions
  • Creating default arguments
  • Creating variable-length argument lists
  • Returning data from functions
  • Returning multiple items from a function
  • Working with text fields, checkboxes, and radio buttons
  • Handling multiple-selection HTML controls
  • Uploading files

This chapter continues your guided tour of PHP with handling PHP functions and HTML controls. PHP functions are just as important in PHP as in JavaScript, but there's some added power in PHP compared to JavaScript. And working with HTML controls is the whole main point of using server-side scripting for many developers. You're going to see both in this chapter.

images For an introduction to using PHP with Ajax, see Chapter 12.

Working with Functions

On the face of it, functions work much the same in PHP as in JavaScript. Here's how a function is set up:

function function_name([argument_list…])
{
    [statements;]
    [return return_value;]
}

It's easiest to see this with examples. For instance, you might want to use a function to display a copyright symbol at the bottom of your Web pages. You would start by displaying the Web page content like this:

<html>
  <head>
    <title>Using functions to create a copyright
mark</title>
  </head>
  <body>
    <h1>Using functions to create a copyright mark</h1>

    <?
      echo “<h3>Welcome to my web page!</h3>”;
      echo ...

Get Ajax Bible 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.