General parameter functions and pseudo-type parameter functions of PHP functions

Source: Internet
Author: User
Tags php programming
In PHP functions, the parameters of the function is the standard for deciding how to successfully apply a function or to control a function's execution behavior. There are many ways to set up and apply a parameter, and it is the key to learn to declare a function with different parameters, and a function that can successfully invoke various forms of the input function. Described earlier How to Declaration and application of various forms PHP functions. This section describes the two parameter types, based on the characteristics of function parameters: general parameter Functions and pseudo-type parameter functions.

PHP General parameter functions

The general parameter function is that the arguments and formal parameters should be equal in number and type, as in the case of a strongly typed language such as C or JAVA. The invocation of such functions is relatively easy, because the flexibility is small, like the strong-class language requirements are more stringent (the number of parameters is fixed, the type of each parameter is also fixed).

The function format description of the general parameters is similar to the following form:

Example (name, age, height)                //function format for general parameters

In PHP, if you declare such a function, you do not have the advantage of PHP as a weakly typed language. For example, in the function syntax format example of the general parameter above, declare a function named example that returns a value of type string after the function executes. The function has three parameters, the number and order of arguments passed must be the same, and the first argument must be a string type, the second argument must be an integral type, and the third argument must be a double-precision type. For example, in a custom function, the sum of the squares of two integers is a function of a regular parameter, and requires that there must be two parameters of integral type. There are many system functions that are this type, and some system functions that use the general parameters are as follows:

string chr (int ascii)          //must use an integer as the parameter of float ceil (float value)       //must use a floating-point number as the parameter array array_combine (array keys, Array values)            //must use two arrays as parameter int strnatcmp (string str1, String str2)          //must use two string as argument string implode (string glue , array pieces)    ///The first argument must be a string, the second argument must be an array of string Readdir (resource Dir_handle)            //must use a resource type as a parameter

Functions for PHP pseudo-type parameters

PHP is a weakly typed language that does not need to specify a type when declaring a variable, nor does it need to specify a type when declaring a function, so each parameter of a function in PHP can pass a value of any type to it. Because the weak type is the largest feature of the PHP language, when declaring a function, you can allow the same parameter to accept any type of value. In strongly typed programming languages such as C or JAVA, if you want to declare a method for sorting arrays, you must write a sort method for each type of array, which is called a function overload. There is no overloaded concept for the weakly typed parameters such as PHP. In PHP, if you sort an array of various types, just declaring a function is enough, so the function of the pseudo-type parameter is the most common form of function application in PHP.

The function format description of the pseudo-type parameter is similar to the following:

Mixed funname (mixed $args)    //The parameter that appears in the parameter list type uses mixed to describe the argument number funname (number $args)    //The parameter that appears in the parameter list type using number description

The pseudo-type of PHP, including mixed, number and callback three kinds. When declaring a function, the parameter type can be marked with mixed in the description document of the function if the parameter can accept many different but not necessarily all types of values. If you specify that a parameter can be integer or float, you can use the number tag parameter. In addition to parameters that can pass pseudo-type parameters, the return value of a function can return different types of values depending on the type of the parameter, such as empty (), POW (), and so on.

For example, the call_user_func () function can receive a user-defined function as a parameter, which is a built-in function of PHP. The callback function can be either a function or a method of an object, and the method of a static class can also be used. A PHP function is passed with a function name string that can pass any built-in or user-defined functions.

Here's a simple example:

<?php$data = Array ("name" = "Callback", "value" = "Test"), $rs 1 = http_build_query ($data);        Call the PHP function directly $rs2  = Call_user_func ("Http_build_query", $data);    Use the callback function echo $rs 1;     Name=callback&value=testecho "<br/>"; Echo $rs 2;     Name=callback&value=test?>

Note: It should be noted here that parameter 1 must be a function that can be used by function_exists () to return true function, which reminds isset,empty,is_null that the common function is actually an operator. It is not a function.

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. PHP programming from getting started to mastering the full set of video tutorials

3. PHP real-Combat video tutorial

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.