[Case Analysis] Detailed explanation of the role of PHP static keyword

藏色散人
Release: 2023-04-09 18:24:02
forward
3735 people have browsed it

Recommended: "PHP Video Tutorial"

Explanation: I have been using PHP language for a while, and today I saw the static keyword in a certain code. Suddenly I thought about what is the difference between static and no static? Let me explain through a case. The requirement is that I specify a directory. Count the "number of directories" and "how many files" in this directory and display the "folders/files" respectively.

1, This is the directory structure to be obtained, a total of (3 directories):

2, I will paste the code directly here, this is the "static" keyword operation result:

It was found that the number of "source directories" is inconsistent with that obtained by the go function. Test results:

##3, after adding the "static" keyword, confirm that the "files and directories" are correct:

Summary: Some people are curious why it was added Is the static keyword correct? That's because the life cycle of the static variable exceeds the function life cycle, and the final variable is still in memory. After removing it, it becomes a local variable and is released after the function is executed, so the saved variable is the result of the last execution!

               

The above is the detailed content of [Case Analysis] Detailed explanation of the role of PHP static keyword. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template