Providing Default Values to Argument Variables

This section provides a tutorial example on how to provide default values to argument variables.

PHP also allows us to provide default values to argument variables using the following syntax on the function definition statement:

   function function_name($arg_1, $arg_2, ... $arg_n=exp) {
   ...
   }

where argument variable, $arg_n, has been assigned with a default value, evaluated from the specified expression, "exp".

Assigning default values to argument variables has several simple rules:

To help us understand how use argument variable default values, I wrote this tutorial example:

<?php 
#  ArgumentDefaultValues.php
#- Copyright (c) 2003-2019, HerongYang.com, All Rights Reserved.
#
   function hello($name="Herong") {
      print("    Hello " . $name . "!\n");
   }

   print("\n Argument with a scalar default value:\n");
   hello();
   hello("World");
   hello(NULL);

   function showList($list=array("Apple", "Orange")) {
      print("    List: " . $list[0] . ", " . $list[1] . "\n");
   }

   print("\n Argument with an array default value:\n");
   showList();
   showList(array("Cat", "Dog"));

#   function showDate($object=new DateTime()) {
#      print("    Year: " . $object->format("Y"));
#   }
?>

If you run this sample script, you should get:

C:\herong> \php\php ArgumentDefaultValues.php

 Argument with a scalar default value:
    Hello Herong!
    Hello World!
    Hello !

 Argument with an array default value:
    List: Apple, Orange
    List: Cat, Dog

If you open last 3 commented-out statements, you will get an execution error: "Parse error: syntax error, unexpected T_NEW in C:\herong\php\ArgumentDefaultValues.php on line 21",

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