Introduction to PHP function examples

Source: Internet
Author: User
Tags function examples types of functions
This article we mainly share with you the PHP function example Introduction, the main simple introduction of the PHP function concept of the use of methods, I hope to help everyone.

Name: Follow the variable naming rules, functions cannot be overloaded,

function : functions implement structured programming and improve the maintainability of code

Global variables: can be used throughout the script.

Local variables: defined in the function body, only used within the function body, the function is automatically released after execution

It is divided into static storage type and dynamic storage type, static variable $a, not released after function execution.

Local variable programming global variables in function: 1.global; 2. $GLOBAL ['] Use global array

Types of functions

1. Functions for Custom parameters:

The function test ($a, $b) {}//test is the functional name, $a and $b are function arguments, and you need to provide the corresponding arguments when the function is called.

2. Functions that refer to parameters:

Function test (& $a) {}//$a is a reference variable, and the external variable is modified if $ A is modified inside the function relative to the external variable.

3. Functions for Default parameters:

Fuction Test ($a =0) {}//$a =0 means that when the function is called, the argument is not passed to the function, then the default $a=0;

4. Functions with variable number of parameters:

function test () {$a, $b,......} {}//principle: Accept all parameters and return a data use through the Func_get_args () function, so you can have multiple parameters

Func_get_args () Gets the parameter array, Func_num_args () Gets the number of arguments, Func_get_arg () returns the specified parameters (typically used in conjunction with Func_num_args)

5. Callback function: function one () {} $fun = "one"; When executing $fun (it is equivalent to invoking the one () function;

6. Recursive function: In the function body, in the call function itself, remember must have an exit, that is, the condition of exit, otherwise it will form a dead loop.

  function test ($a) {                                 if ($a >=0) {   //exit recursive condition, be sure to have                                       test ($a-1);                                } else{                                    return;                                }                       }

7. Anonymous function: is a function without a name, generally used for the callback function. Fucntion ($a) {}

Common functions:

Echo () "Language structure" print ()//"Language Structure" "has a return value", if the transmission fails to cause no output, it returns Falsevar_dump () Var_export () "has a return value, translates a valid PHP code" printf ()//similar to C The form of the language printf ("My name is%s, age%d", $name, $age), print out sprintf ()//similar to printf, but not print, but return formatted text, the rest is the same as printf
Related Article

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.