Common and pseudo-type parameter functions of php functions

Source: Internet
Author: User
In php functions, function parameters are the criteria for determining how to successfully apply a function or control the execution behavior of a function. Parameters can be set and applied in multiple ways. learning to declare functions with different parameters and successfully call functions with various types of parameters is the key to learning functions. In php functions, function parameters are the criteria for determining how to successfully apply a function or control the execution behavior of a function. Parameters can be set and applied in multiple ways. learning to declare functions with different parameters and successfully call functions with various types of parameters is the key to learning functions. The previous section describes how to declare and apply various forms of php functions. This section describes two parameter types based on the features of function parameters: Common parameter functionsAnd Pseudo-type parameter functions.

Common php parameter functions

The common parameter function is that the real parameters and the form parameters should have the same number and type, just like how to use parameters in strong language such as C or JAVA. This type of function is easy to call, because it is not flexible and requires strict requirements like strong languages (the number of parameters is fixed, and the type of each parameter is also fixed ).

The format of common parameters is similar to the following:

Example (name, age, height) // function format of common parameters

In php, such a function cannot take advantage of PHP as a weak language. For example, in the preceding example of the regular function syntax format of the parameter, declare a function named example and return a value of the string type after the function is executed. This function has three parameters. the number and sequence of parameters passed during the call must be the same, the first parameter must be of the string type, and the second parameter must be of the integer type, the third parameter must be of double precision type. For example, in a user-defined function, the sum of squares of two integers is a common parameter function, which requires two integer parameters. Many system functions are of this type. some system functions that use common parameters are as follows:

String chr (int ascii) // An integer must be used as the float ceil (float value) // a floating point number must be used as the parameter array array_combine (array keys, array values) // Two arrays must be used as the parameter int strnatcmp (string str1, string str2) // two strings must be used as the parameter string implode (string glue, array pieces) // The first parameter must be a string, and the second parameter must be an array string readdir (resource dir_handle) // A resource type must be used as the parameter.

Functions of php pseudo-type parameters

PHP is a weak type language. you do not need to specify the type when declaring variables, nor do you need to specify the type when declaring functions, therefore, every parameter of the function in PHP can pass any type of value for it. Weak types are the biggest feature of PHP. when declaring a function, the same parameter can accept any type of value. In C or JAVA, if you want to declare a method for sorting arrays, you must write a sort method for each type of array, this is the so-called function overload. PHP and other weak parameters do not have the concept of overload. In PHP, if you sort arrays of various types, you only need to declare a function. Therefore, functions with pseudo-type parameters are the most common function application form in PHP.

The format of pseudo-type parameters is similar to the following:

Mixed funName (mixed $ args) // The number funName (number $ args) parameter of the type described by mixed is displayed in the parameter list // the parameter of the type described by number is displayed in the parameter list

PHP pseudo-type, includingMixed, number, and callbackThree. When declaring a function, if the parameter can accept multiple different but not all types of values, you can use mixed to mark the parameter type in the function description document. If a parameter can be an integer or floating point type, you can use number to mark the parameter. In addition to passing pseudo-type parameters, function return values can also return different types of values based on different parameter types, such as empty () and pow.

For exampleCall_user_func ()A function can receive user-defined functions as a parameter. it is a built-in function of php. The callback function can be either a function or an object method or a static class method. A php function is passed using a function name string. it can pass any built-in or user-defined function.

Here is a simple example:

 "Callback", "value" => "test"); $ rs1 = http_build_query ($ data); // directly call the php function $ rs2 = call_user_func ("http_build_query ", $ data); // use the callback function echo $ rs1; // name = callback & value = testecho"
"; Echo $ rs2; // name = callback & value = test?>

Note: parameter 1 must be a usable function that can return true through function_exists (). The isset, empty, is_null and other common functions are actually an operator. it is not a function.

[Recommended tutorials]

1. php1.cn Dugu jiujian (4)-php video tutorial

2. php programming from getting started to mastering a full set of video tutorials

3. php video tutorial

These are the details of common and pseudo-type parameter functions of php functions. For more information, see other related articles in the first PHP community!

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.