search
HomeBackend DevelopmentPHP ProblemYou must understand the parameters of the function (in detail)

The last article introduced you to "Let’s talk about the difference between the return statement and echo (detailed explanation and examples)". This article continues to introduce the parameters of the function to you. Now let’s go and take a look. Look! ! !

You must understand the parameters of the function (in detail)

Parameters of function (actual parameters and formal parameters):

Formal parameter variables only allocate memory units when called , at the end of the call, the allocated memory unit is released immediately. Therefore, formal parameters are only valid within the function. After the function call ends and returns to the calling function, the formal parameter variable can no longer be used.

Actual parameters can be constants, variables, expressions, functions, etc. No matter what type of quantities the actual parameters are, they must have definite values ​​when making function calls so that these values ​​can be transferred to formal parameters. Therefore, assignment, input, etc. should be used in advance to obtain a certain value for the actual parameters.

The actual parameters and formal parameters should be strictly consistent in number, type, and order, otherwise a "type mismatch" error will occur.

The data transfer that occurs in function calls is one-way. That is, only the value of the actual parameter can be transferred to the formal parameter, but the value of the formal parameter cannot be transferred in the reverse direction to the actual parameter. Therefore, during the function call, the value of the formal parameter changes, but the value of the actual parameter does not change.

Simply put, parameters are divided into two parts: formal parameters and actual parameters:

Formal parameters are formal parameters, used in parentheses when defining functions;

The actual parameter is the actual parameter, used in the parentheses of the calling function;

For example, int F(int i) i is the formal parameter,
The actual parameter is the function call When there are parameters passed by the calling function to the called function, for example:

int x=1;
a=F(x);


x here is the actual parameter. After the function is called, the value of x is passed to the formal parameter. i

Note:

  • If the function has defined parameters, the corresponding actual parameters must be passed in when calling the function (When the formal parameter has no default value)

  • If the function also has the default value of the formal parameter and the corresponding actual parameter is passed in, then the passed actual parameter will be the main one (equivalent to For variable assignment, use the value of the actual parameter to assign the value to the formal parameter)

The specific operation takes the code as an example:

<?php
     /******函数的参数 */
     function table (){
         $str = &#39;<table border="1" align="center" width="800">&#39;;
         for ($tr = 0; $tr < 10; $tr ++){
             $str .= &#39;<tr>&#39;;
             for($td = 0; $td < 10; $td ++){
                $str .= &#39;<td>&#39;.$tr.$td. &#39;</td>&#39;;
            }
            $str .= &#39;</tr>&#39;;
         }
         $str .= &#39;</table>&#39;;
         //返回值  返回一个表格
         return $str;
     }
     $table = table();
     echo $table;
?>

The demonstration results are as follows:

You must understand the parameters of the function (in detail)Code analysis:

We use the function function to define a table, and then set the specifications of the table (supplementary: align refers to adjustment, calibration, center: center), and then set Define the for loop, enter the for loop and continue to set the second for loop statement. The specific code is as shown above. At the end, we use return to return the value (the purpose is to determine whether the return statement can return a value successfully and whether it can return a table. ), when we return a table, call the table statement, {$table=table()}, output (table). Overall, we first define a variable (table), connect one (tr tag) in the table, and then connect 10 (td). At this time, the loop ends, and we connect an end flag (/td). At this time, we continue Loop, and so on;

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of You must understand the parameters of the function (in detail). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)