Returning Values from Functions

This section provides a tutorial example on how to return values from functions - using the 'return' statement.

PHP supports function return values with some basic rules:

To help us understand these rules, I wrote this tutorial example:

<?php # FunctionReturnValues.php
#- Copyright (c) 2015,, All Rights Reserved.

   function returnType($arg) {
      if (is_null($arg)) return "NULL";
      elseif (is_bool($arg)) return "BOOLEAN";
      elseif (is_int($arg)) return "INTEGER";
      elseif (is_float($arg)) return "FLOAT";
      elseif (is_string($arg)) return "STRING";
      elseif (is_array($arg)) return "ARRAY";
      elseif (is_object($arg)) return "OBJECT";
      elseif (is_resource($arg)) return "RESOURCE";
      else return "UNKNOWN";      
   print("\n Returning a scalar value:\n");
   print("    1: " . returnType(1) . "\n");
   print("    1.0: " . returnType(1.0) . "\n");
   print("    array(1.0): " . returnType(array(1.0)) . "\n");

   function createArray() {
      return func_get_args();
   print("\n Returning an array value:\n");
   print("    createArray(): ".returnType(createArray(3,6,9))."\n");

   function currentDate() {
      return new DateTime();
   print("\n Returning an object value:\n");
   print("    currentDate(): " . returnType(currentDate()) . "\n");

   function noReturn() {
   print("\n Function without return statements:\n");
   print("    noReturn(): " . returnType(noReturn(3,1,4)) . "\n");

If you run this sample script, you should get:

 Returning a scalar value:
    1: INTEGER
    1.0: FLOAT
    array(1.0): ARRAY

 Returning an array value:
    createArray(): ARRAY

 Returning an object value:
    currentDate(): OBJECT

 Function without return statements:
    noReturn(): NULL

Last update: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 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

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 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

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Outdated Tutorials


 PDF Printing Version