What is the concept of a callback function? , how do I use a custom callback function in PHP?

Source: Internet
Author: User
User-defined functions are also called custom functions, which are not provided by PHP and are created by programmers. Because you create such a function, you have complete control over the functions. So you can have a function that works exactly as you want it to.

1, declaration function in PHP, the method of defining a function is almost the same as in other programming languages. The following is the syntax structure of the PHP declaration function:

function function_name ($argument 1, $argument 2, $argument 3,...... $argumentn) {//Functions code Codereturn return value;} In the syntax structure above, the meanings of the keywords are as follows.

(1) Function: A keyword used to declare a user-defined function.

(2) Function_name: The name of the function to be created. The name will be used when it is called later. The function name should be unique because PHP does not support overloading. When naming functions, you need to follow the same principle as the variable name. But the function name cannot start with $, and the variable can

. (3) Argument: The value to pass to the function. A function can have multiple parameters, with commas between them. However, parameter entries are optional and can be passed without any arguments when calling a function.

(4) Code: This is a section of the codes that executes when a function is called. If there are two or more statements, the code must be enclosed in curly braces "{}". However, if there is only one code, no curly braces are required.

(5) Return: Returns the value required by the calling code. Any type can be returned, including lists and objects. This causes the function to immediately end its run and pass control back to the row it was called

.2, parameter-free function

Code func_1.php

<?phprequire ' a.php '; echo "I will not be executed!";? >

3, Parameter function PHP supports passing parameters by value (default), passing by reference and default parameter values. variable-length parameter lists are supported only in PHP4 and successors . 1) value passing parameters. Parameter passing by value is the default way to pass PHP. Using this method, you must pass a value (parameter) when the main program is called .

Code sum.php

<?phpfunction sum ($a, $b) {     Echo $a + $b;} SUM (100,20);   Start calling the function?>

(2) Reference parameters. When passed by value, only a copy of the parameter is passed to the called function. However, any modification of these values within the called function does not affect the original value in the calling function. The reference pass is actually the address pass, which passes the address of a variable as an argument. Code

valuechange.php

<?php$mynum=100;function Valuechange ($number) {     $number = $number +1; Echo $number. "<br>";} Valuechange ($myNum); Echo $myNum;? >

(3) default value parameter. Using the default parameter value delivery method, the function must have a parameter at the time of invocation. If no value is used, the default value is passed to the function parameter. The default value must be a constant expression, not a variable, a class member, or a function call. Tip: When using default parameters, Any default parameter must be placed to the right of a non-default parameter, otherwise the function may not work as expected.

Code func_default.php

<?php$mynum=100;function Valuechange ($number) {$number = $number +1; Echo $number. "<br>";} Valuechange ($myNum); Echo $myNum;? 

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.