PHP Tutorials - Herong's Tutorial Examples - Version 4.20, by Dr. Herong Yang
Usage Examples of Functions
This section provides usage examples of functions.
Here is a PHP script that provides usage examples of functions:
<?php # FunctionTips.php #- Copyright (c) 2003-2019, HerongYang.com, All Rights Reserved. # function msg() { print("Hello world!\n"); } msg(); print("\n"); function hello($f) { print("Hello $f!\n"); } hello("Bob"); print("\n"); function getYear() { $year = date("Y"); return $year; } print("This year is: ".getYear()."\n"); print("\n"); function f2c($f) { return ($f - 32.0)/1.8; } print("Celsius: ".f2c(100.0)."\n"); print("Celsius: ".f2c(-40.0)."\n"); print("\n"); function swap($a, $b) { $t = $a; $a = $b; $b = $t; } $x = "PHP"; $y = "JSP"; print("Before swapping: $x, $y\n"); swap($x, $y); print("After swapping: $x, $y\n"); print("\n"); # Passing arguments by value with references not allowed any more # $x = "PHP"; # $y = "JSP"; # print("Before swapping: $x, $y\n"); # swap(&$x, &$y); # print("After swapping: $x, $y\n"); # print("\n"); function ref_swap(&$a, &$b) { $t = $a; $a = $b; $b = $t; } $x = "PHP"; $y = "JSP"; print("Before swapping: $x, $y\n"); ref_swap($x, $y); print("After swapping: $x, $y\n"); print("\n"); function average($array) { $sum = array_sum($array); $count = count($array); return $sum/$count; } $numbers = array(5, 7, 6, 2, 1, 3, 4, 2); print("Average: ".average($numbers)."\n"); print("\n"); function shrink($array) { array_splice($array,1); } $numbers = array(5, 7, 6, 2, 1, 3, 4, 2); print("Before shrinking: ".join(",",$numbers)."\n"); shrink($numbers); print("After shrinking: ".join(",",$numbers)."\n"); print("\n"); # Passing arguments by value with references not allowed any more # $numbers = array(5, 7, 6, 2, 1, 3, 4, 2); # print("Before shrinking: ".join(",",$numbers)."\n"); # shrink(&$numbers); # print("After shrinking: ".join(",",$numbers)."\n"); # print("\n"); function ref_shrink(&$array) { array_splice($array,1); } $numbers = array(5, 7, 6, 2, 1, 3, 4, 2); print("Before shrinking: ".join(",",$numbers)."\n"); ref_shrink($numbers); print("After shrinking: ".join(",",$numbers)."\n"); print("\n"); function powerBall() { $array = array(rand(1,55), rand(1,55), rand(1,55), rand(1,55), rand(1,55), rand(1,42)); return $array; } $numbers = powerBall(); print("Lucky numbers: ".join(",",$numbers)."\n"); print("\n"); function myPassword() { $password = "U8FIE8W0"; print("Defined inside the function? ". isset($password)."\n"); } myPassword(); print("Defined outside the function? ". isset($password)."\n"); print("\n"); $login = "fyicenter"; function myLogin() { print("Defined inside the function? ". isset($login)."\n"); } myLogin(); print("Defined outside the function? ". isset($login)."\n"); print("\n"); $intRate = 5.5; function myAccount() { global $intRate; print("Defined inside the function? ". isset($intRate)."\n"); } myAccount(); print("Defined outside the function? ". isset($intRate)."\n"); print("\n"); $favor = "Blue"; function getFavor() { global $favor; return $favor; } $myFavor = getFavor(); print("Favorite tool: $myFavor\n"); $favor = "Pink"; print("Favorite tool: $myFavor\n"); print("\n"); $favor = "Blue"; function &getFavorRef() { global $favor; return $favor; } $myFavor = &getFavorRef(); print("Favorite tool: $myFavor\n"); $favor = "Pink"; print("Favorite tool: $myFavor\n"); print("\n"); function printKey($key="download") { print("PHP $key\n"); } printKey(); printKey("hosting"); print("\n"); function myAverage() { $count = func_num_args(); $args = func_get_args(); $sum = array_sum($args); return $sum/$count; } $average = myAverage(102, 121, 105); print("Average 1: $average\n"); $average = myAverage(102, 121, 105, 99, 101, 110, 116, 101, 114); print("Average 2: $average\n"); print("\n"); ?>
If you run this sample script, you should get:
C:\herong> \php\php FunctionTips.php Hello world! Hello Bob! This year is: 2019 Celsius: 37.777777777778 Celsius: -40 Before swapping: PHP, JSP After swapping: PHP, JSP Before swapping: PHP, JSP After swapping: JSP, PHP Average: 3.75 Before shrinking: 5,7,6,2,1,3,4,2 After shrinking: 5,7,6,2,1,3,4,2 Before shrinking: 5,7,6,2,1,3,4,2 After shrinking: 5 Lucky numbers: 48,16,54,26,2,30 Defined inside the function? 1 Defined outside the function? Defined inside the function? Defined outside the function? 1 Defined inside the function? 1 Defined outside the function? 1 Favorite tool: Blue Favorite tool: Blue Favorite tool: Blue Favorite tool: Pink PHP download PHP hosting Average 1: 109.33333333333 Average 2: 107.66666666667
Last update: 2019.
Table of Contents
Introduction and Installation of PHP 7.3
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
"function" Statements - Defining Functions
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
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