PHP Setting default values for user-defined function parameters in

2012-07-07 13:23:00|   classification : php Custom function , Silent | report | Font size   subscribe


php When a user-defined function accepts a parameter, it can directly assign a default value to it

function TestStr($str)
return $str." Is a string ";

echo TestStr("abcdef")."</br>";

The above custom function takes a parameter , If we call TestStr() ; What happens if you don't give the parameters ?

Direct error :Warning: Missing argument 1 for TestStr()

So in the process of development, in order to avoid the above errors, we generally need to assign default values to parameters when we customize functions , as follows

function TestStr2($str="kkkkk")
return $str." Is a string ";

Now call TestStr2() Don't go wrong , Direct output kkkkk Is a string , Of course, the default parameters will be replaced after you send out the parameters .

The third case :

Yes 2 With more than one parameter , If not all the default values are given , Be sure to give the default values from right to left . Here are some examples of mistakes

function TestStr3($str ='mmmmm' ,$para)
return $str." and ".$para." All strings ";

The above doesn't follow from right to left to give the default value , But the one on the far left gives a default value .

When we call a function TestStr3(" The first parameter "); What we expect is that the first parameter is replaced by the default value , The second parameter is replaced by an incoming parameter . But it's not like that , The order of replacing parameters is from left to right . So not only does the second parameter have no value passed in , Not even a default value , So report a mistake directly

The correct default value should be like this

function TestStr3($str,$para='mmmmm')
return $str." and ".$para." All strings ";

Then the output :

The first parameter and mmmmm All strings

php Function parameters are assigned from left to right

  1. php Function parameter

    The parameters of the function You can pass information to functions through parameter lists , A list of expressions separated by commas . Parameters are evaluated from left to right . PHP Support passing parameters by value ( Default ), Pass parameters and default parameters by reference . Variable length parameter lists are also supported , For more information, see ...

  2. Python Record 6: function 2, Function parameter

    # One . Function parameters fall into two broad categories # Shape parameter : In the function definition phase, the parameters defined in brackets ( Variable name ), It's called a formal parameter         # Because the function definition stage just uses a symbol to represent a parameter , There is no specific value , So it's called a formal parameter , shape ...

  3. c What is the order in which function parameters are put on the stack ? Why?

    See the interview questions C How to stack function parameters in the language ? I don't know , Looking for information on the Internet . Here's a detailed explanation #include <stdio.h> void foo(int x, int y, int z){   ...

  4. Day04: Function parameter 、 object 、 nesting 、 Closure functions and decorators

    Review last class :    1. What is a function         Function is a tool with a certain function     2. Why use functions         1. The organization and readability of the program         2. Reduce code redundancy         3. Extensibility is strong ...

  5. C Why is the stack order of language function parameters from right to left ?( From left to right , meet printf It's going to be a dead cycle )

    I learned assembly language last semester , And in the experiment of operating system, assembly is used +C Mixed language programming , I also learned something about it C The corresponding relationship between assembly language and language . Assembly language is the bottom programming language , All kinds of function parameters need to be accessed directly by the control stack , In hybrid programming , We need to use foreign exchange ...

  6. printf: Function parameters are evaluated from right to left , From left to right ?

    Giant panda making refrigerator @cnblogs 2019/8/3 1. problem One day I wrote the following code : unsigned char ReadByteFromFile ( FILE * fp ) { unsigned char ...

  7. Delphi_08_Delphi_Object_Pascal_ Basic grammar _06_ Function parameter

    Find out Delphi There are many contents about function parameters in , For the time being, let's stop with this essay , Let's continue to discuss the parameter part of the function later . One Engineering documents program DefaultParameter; {$APPTY ...

  8. Matlab in plot Function parameter analysis

    function Two dimensional curve drawing grammar plot(Y) plot(X1,Y1,...) plot(X1,Y1,LineSpec,...) plot(...,'PropertyName',PropertyValue, ...

  9. C++ Two dimensional array ( Double pointer as function parameter )

    The learning content of this article is for reference : ...

Random recommendation

  1. Andrew Ng Machine learning open class notes -- Learning theory

    Netease public class , The first 9,10 course notes, The problem to be discussed in this chapter is , How to evaluate and choose learning algorithms   Bias/va ...

  2. Django Integrate celery Practical small projects

    That was covered in the last post celery Basic knowledge of , This article takes a small project as an example , Detailed instructions django How framework integrates celery Development . The development environment for this series of articles : window 7 + python2.7 + pychar ...

  3. .net Type conversion

    stay .net  There are two ways of type conversion under the platform , Force or as transformation .( Some people say that forced transfer is carried out in both ways , There are two ways of forced rotation ). Such as :typeA objA = (typeA)objB Or use another typeA ...

  4. [springBoot series ]--springBoot A complete set of annotations [ turn ]

    One . annotation (annotations) list @SpringBootApplication: Contains @ComponentScan.@Configuration and @EnableAutoConfiguration ...

  5. How to be in java List Fuzzy query in

    For example, I have the following one List, It's a collection of Employee object . And then I want to talk about this List Carry on according to Employee Object name for fuzzy query . Is there any good solution ? For example, the query condition I entered is “wang”, that ...

  6. 4.python String formatting

    When formatting a string ,Python Use a string as a template . There are formants in the template , These formants reserve places for real values , And explain the format that the real value should present .Python Use one tuple Passing multiple values to the template , Each value corresponds to a formatter .py ...

  7. JAVA frame Mybaits

      Be careful : We are resultType in , about selectlist Method is also projo class .resultType The meaning of the parameter is list The generic type of . One :jar Package download : ...

  8. object-oriented Java practice

    package xin.bao; public class Pingguo { private String Zhonglei;// species public String getZhonglei() { ...

  9. openfire database mysql To configure

    <?php return array( //' Configuration item '=>' Configuration values ' //'USERNAME'=>'admin', // assignment // Database configuration information 'DB_TYPE' => 'm ...

  10. MySQL And ——GROUP BY Group to get the maximum value of the field

    Reprinted from : Suppose there is a business scenario , Need to query user login record information , The table structure is as follows : CREATE ...