Differences between passing variable parameters to functions in JS and PHP instance code

Source: Internet
Author: User
The difference between passing variable parameters to functions in JS and PHP is the instance code. For more information, see. # JS call function to pass variable parameters

The code is as follows:


Script
Function test (){
For (var I = 0; I <arguments. length; I ++ ){
Alert (arguments [I]);
}
}
// Call a function
Test (1, 2, 3, 'ABC ');
Script


# PHP call the function to pass variable parameters

The code is as follows:


// Method 1
// Receives a series of parameters and outputs them one by one
Function show_params (){
// Obtain the number of passed parameters
$ Count = func_num_args ();

// Traverse parameters and output them one by one
For ($ I = 0; $ I <$ count; $ I ++ ){
// Obtain parameters
$ Param = func_get_arg ($ I );
Echo $ param. PHP_EOL;
}
}

// Call a function
Show_params (1, 2, 'apple', 3.14 );

// Method 2
Function show_params (){
// Define an array for storing passed parameters
$ Params = array ();
// Obtain all parameters
$ Params = func_get_args ();
$ Count = count ($ params );
// Traverse and output parameters one by one
For ($ I = 0; $ I <$ count; $ I ++ ){
Echo $ params [$ I];
Echo PHP_EOL;
}
}
// Note: Method 2 is slower than method 1.

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.