


PHP passes and receives parameters in the function body_PHP tutorial
In PHP functions, parameter passing can be divided into two types: value passing and reference passing (also called address passing).
By default, PHP passes parameters by value. Pass-by-value parameters When a function is called, the value of a constant or variable (usually called an actual parameter) is passed to the function's parameter (usually called a formal parameter). The characteristic of value transfer is that the implementation parameters are stored in memory separately and are two unrelated independent variables. Therefore, when the value of the formal parameter is changed inside the function, the value of the actual parameter generally does not change.
The characteristic of passing by reference (passing by address) is that the implementation and row parameters share a memory. Therefore, when the value of the formal parameter changes, the value of the actual parameter will also change accordingly. From this perspective, the formal parameters and actual parameters can be considered to be the same variable.
When defining parameters to be passed by reference, you can add the reference symbol & in front of the parameter.
<?php function printString(&$string){ echo($string); $string="打印完成"; } $str="测试字符!n"; printString($str); echo($str); ?> // 程序输出:测试字符! 打印完成
php also supports variable length parameter lists. When defining a function, no parameters are specified. When calling a function, you can specify the number of parameters as needed, and obtain parameter information through several system functions related to the parameters. The specific instructions are:
<?php function mysum() { $num = func_num_args(); echo("函数包含:".$num."个参数n"); $sum = 0; for($i=0; $i < $num; $i++) { $sum = $sum + func_get_arg($i); } echo("参数累加纸盒为:".$sum); } mysum(1,2,3,4); ?> // 程序输出:函数包含:4个参数 参数累加纸盒为:10
Func_num_args function function: Returns the number of parameters passed to the function. Its syntax is as follows: int func_num_args (void). Description: Returns the number of parameters passed to the currently defined function. func_get_arg() will generate a warning if this function is called from outside the function definition.
func_num_args( ) can be used in conjunction with func_get_arg( ) and func_get_args( ) to allow user-defined functions to accept variable-length argument lists. Among them, func_get_arg() returns items from the parameter list, its syntax: int func_get_arg (int arg_num), returns the arg_numth parameter of the parameter list that defines the function, and its parameters start from 0. And calling this function outside the function definition will generate a warning; and when arg_num is greater than the number of parameters actually passed by the function, a warning will also be generated and FALSE will be returned.
The difference between the func_get_args() function and the func_get_arg() function is that the func_get_args() function returns an array, and each element of the array is equivalent to the number of parameter columns of the current user-defined function.
When we build a PHP class, flexibly using these three functions can achieve very ideal results. For example, when creating a class that links PHP and MYSQL, you can write the following code:
<?php class mydb { private $user; private $pass; private $host; private $db; public function __construct(){ $num_args=func_num_args(); if($num_args>0){ $args=func_get_args(); $this->host=$args[0]; $this->user=$args[1]; $this->pass=$args[2]; $this->connect(); } } } ?>
Give another example program:
<?php function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br>n"; if ($numargs >= 2) { echo "Second argument is: " . func_get_arg (1) . "<br>n"; } $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo "Argument $i is: " . $arg_list[$i] . "<br>n"; } } foo (1, 2, 3); ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
