PHP notes: Functions used in PHP

Source: Internet
Author: User
Tags echo b mixed

The Declaration of a function
1. Grammatical structure

The code is as follows Copy Code

function functions name (argument list)
{
function body; Optional
return value; Return optional
}

parameter list separated by commas, do not omit the $ variable symbol
Function names are case-insensitive, rules are consistent with variables
function call to execute, you can first invoke the post declaration
Function name cannot be duplicate, including system function

2. Function call

The code is as follows Copy Code

function sum ($x, $y)//formal parameter: arguments declared when declaring a function
{
$x = 1; If you assign a value to a parameter within a function, the argument is overwritten.
$y = 2;
$sum = 0;
$sum = $x + $y;
return $sum; Execution to return function end, no longer executed after
}

SUM ();
SUM (2,2); Arguments: Values passed to the formal parameter when the function is called
echo sum (2,2); Returning data by return can be used as a value


Ii. variables in the function
1. Variable Range

Local variables: variables declared and used within a function
Global variables: Declared outside the function, the script can be used anywhere

Tips: Use global variables in PHP functions to include this global variable in a function through the Global keyword in order to use

The code is as follows Copy Code

$a = 1;
$b = 2;
function demo ($a = ')
{
Global must be placed in front
Global $a, $b Global can contain multiple globally variable
$GLOBALS [' a ']//declaring global variable method two
Global $a;
$a + 5;
echo $a;
}

Demo ();


2. PHP static variable

Static variables can be invoked multiple times in the same function
A static variable can only be declared in a function or class and cannot be declared globally.

The code is as follows Copy Code

function demo ($a = ')
{
static $a = 0; Defining static variables
$a + +;
echo "$a <br>";
}
Demo (); Output 1
Demo (); Output 2


3. Variable function

The code is as follows Copy Code

$var = ' Hello '; After the variable name, look for a function with the same name as the variable value
$var (); Equivalent to Hello ();


Classification and declaration of functions

1. Function classification

The code is as follows Copy Code

General functions
BOOL Copy (string source,string dest)

With mixed, indicating that any type of data can be transmitted
BOOL Chown (string filename,mixed user)

function with & parameter, this parameter can only be a variable, the value of the variable is dynamic
Array sorting, index retention of arrays, and cell associations
BOOL Arsort (array & $array [, int $sort _flags])
$fruits = Array (' A ', ' C ', ' B ');
Arsort ($fruits);
Var_dump ($fruits);

The default function, which is a function with a bracket in it, that indicates that the parameter is optional, and that the default value is used if no value is passed
If the call is not assigned, and there is no initial value, a warning appears
Function demo ($a =1, $b =2)
{
#code
}
Demo ();

With... parameter that represents any number of arguments that can be passed
Inserts one or more cells at the beginning of an array
int Array_unshift (array & $array, mixed $var [, mixed $ ...])

2. Declaring multiple parameter functions

The code is as follows Copy Code

function Test ()
{
Echo Func_num_args ();
}
Test (' A ', ' B ', ' C '); 3

Func_get_arg (): Returns the nth argument in the argument list

function Test ()
{
Echo func_get_arg (' 1 ');
}
Test (' A ', ' B ', ' C '); B

Working with instances

The code is as follows Copy Code

function Cls_mysql ()
{
$mysql = Func_get_args ();
$conn = mysql_connect ($mysql [' 0 '], $mysql [' 1 '], $mysql [' 2 ']);
}
Cls_mysql (' localhost:3306 ', ' root ', ' 123456 ');

3, Function Annotation specification

The code is as follows Copy Code
/**
* Use the Demo function/function function to decide whether to use
*
* @static
* @access Public
* @param string $attributeName property name//function argument, deciding how to call the
* @return The return value of string//function, decide what to do after the call
*/
Public Function Demo ($attributeName)
{
$attributeName = ' abc ';
Return $attributeName
}


Four, Callback callback function

We customize a function A, but instead of going directly to the function A, we call function A in function B, and function B implements a call to function A by receiving the name and parameters of function A, which is called a callback function.

The code is as follows Copy Code

function A ($x, $y)
{
return $x * $y;
}
function B ($a, $b, $fun)
{
Return $a + $b + $fun ($a, $b);
}
Echo B (2,3, "A");


Implementation method of PHP callback function

PHP provides two built-in functions Call_user_func () and Call_user_func_array () provide support for callback functions

The code is as follows Copy Code

Mixed Call_user_func_array receives the parameters of the callback function as an array
Mixed Call_user_func_array (callable $callback, array $param _arr)
Call_user_func_array (' F1 ', Array (' A ', ' B '));

The number of call_user_func parameters is determined according to the parameters of the callback function
Mixed Call_user_func (callable $callback [, Mixed $parameter [, mixed $ ...]]
Call_user_func_array (' F1 ', ' A ', ' B ');


Instance:

The code is as follows Copy Code

Common global functions
Function F1 ($arg 1, $arg 2)
{
echo $arg 1. " <br> ". $arg 2." <br> ";
}

echo "Calls function f1:<br> via Call_user_func_array";
Call_user_func_array (' F1 ', Array (' A ', ' B '));

echo "Calls function f1:<br> via Call_user_func";
Call_user_func (' F1 ', ' C ', ' D ');

Class
Class MyClass
{
Public $name;
Function Show ($arg 1)
{
Echo ' parameter name: '. $arg 1. ' <br> ";
Member name in echo ' object: '. $this->name;
echo "<br>";
}
function Show1 ($arg 1, $arg 2)
{
echo $arg 1. " <br> ". $arg 2." <br> ";
}
public static function Show2 ($arg 1, $arg 2)
{
echo $arg 1. " <br> ". $arg 2." <br> ";
}
}

Echo invokes the Non-static member function in the class, which has this called the member:<br> in the object.
$a = new MyClass;
$a->name = ' F ';
Call_user_func_array (Array ($a, ' show '), Array (' E '));

Echo invokes the Non-static member function of the class, which has no members in the calling object, no this:<br>.
Call_user_func_array (Array (' MyClass ', ' Show1 '), Array (' G ', ' H '));

echo "Calls static member function:<br> in a class";
Call_user_func_array (Array (' MyClass ', ' show2 '), Array (' I ', ' J '));

Five, internal functions

The function is declared inside the function to be called inside the function to help the external function to complete some child functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.