Home >Backend Development >PHP Tutorial >[Case Analysis] Detailed explanation of the role of PHP static keyword
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!