The corresponding relationship between the PHP parameter and the argument

Source: Internet
Author: User
Keywords Php
  function myfunction($num,$a,$b,$c) {                   echo "a".$a;                   echo "-b".$b;                   echo "-c".$c;                   echo "
"; $b+=2; $c+=3; $a+=1; echo "a".$a; echo "-b".$b; echo "-c".$c; echo "-num".$num; } myfunction(100,1,2,3); 运行结果:

A1-b2-c3
a2-b4-c6-num100

My inference is that the function is a one by one-corresponding relationship between the run-time formal parameter and the argument

But when I only want to pass $num $a $c These three parameters do not want to pass $b should be if the argument

MyFunction (100,1,3);

Operation Result:
A1-b3-c
a2-b5-c3-num100

I'm actually trying to make $a =1 $c =3 don't $b.

Reply content:

  function myfunction($num,$a,$b,$c) {                   echo "a".$a;                   echo "-b".$b;                   echo "-c".$c;                   echo "
"; $b+=2; $c+=3; $a+=1; echo "a".$a; echo "-b".$b; echo "-c".$c; echo "-num".$num; } myfunction(100,1,2,3); 运行结果:

A1-b2-c3
a2-b4-c6-num100

My inference is that the function is a one by one-corresponding relationship between the run-time formal parameter and the argument

But when I only want to pass $num $a $c These three parameters do not want to pass $b should be if the argument

MyFunction (100,1,3);

Operation Result:
A1-b3-c
a2-b5-c3-num100

I'm actually trying to make $a =1 $c =3 don't $b.

The function parameter is one by one corresponding, if you do not want to pass a parameter can set the default value

function myfunction($num=0,$a=0,$b=0,$c=0){...}//调用函数位置要留出来myfunction(100,,1,2);

The title of the main summary of the right, 实参和形参就是一一对应的关系 .
If you have three formal parameters, but you just want to pass the first and third arguments, corresponding to the first and third of the formal parameters, then use the method upstairs;

There is another, is generally, the non-commonly used formal parameters to put the last, and give that parameter a default value ( This value is based on business logic ), so you pass the parameters only pass the first few parameters you want to pass, you can ignore the following.

When the function is defined, the corresponding parameter is the formal parameter, which corresponds to you here is-function myfunction ($num, $a, $b, $c) {}.
When the function is called, the corresponding arguments are, arguments, corresponding to you here is-myfunction (100,1,2,3).

must correspond to each other.

When you write a function, how many parameters are defined, how many parameters will be written when the call is made, and the principle is the same as the queue name, you empty one, if you do not leave a position to explain is vacant, it will let the back of the people on top, resulting in the final position vacancy

  • 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.