What Is a Function

This section describes what is a function. A quick example is provided showing how to define and call a function.

What Is a Function? A function is a definition of a block of statements. The block of statements in a function will be executed only when the function is invoked. PHP provides a long list of built-in (predefined) functions. PHP supports user-defined functions - You define your own functions in PHP source code.

To learn how to use user-defined functions in PHP, we need to pay attentions in following areas:

1. How to define a function?

2. How to call a function to execute its statements?

3. How to pass input values into a function?

4. How to return a result from a function?

5. How to share variables with a function?

Here is a simple example script of defining a function, calling a function, passing data into a function, and returning result from a function:

   function square($x) {
      $y = $x * $x;
      return $y;
   }

   $radius = 2.0;
   $area = M_PI * square($radius);
   echo($area);

Notice that in this example script:

Last update: 2019.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

Function Declaration, Arguments, and Return Values

What Is a Function

 "function" Statements - Defining Functions

 Function Call Operations

 Passing Arguments to Functions

 Example of Passing Arguments by Values

 Using Pass-by-Value Arguments for References

 Example of Passing Arguments by References

 Variable-Length Argument Lists

 Providing Default Values to Argument Variables

 Returning Values from Functions

 Returning References from Functions

 Usage Examples of Functions

 Arrays - Ordered Maps

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Configuring and Sending out Emails

 Outdated Tutorials

 References

 Full Version in PDF/EPUB