Use PHP to develop robust Code 2 to effectively use variables. "Developing robust code with PHP" is a series of articles on solving practical problems in large and medium-sized applications. In this article, PHP veteran AmolHatwar discusses how to effectively use variables. "Developing robust code with PHP" is a series of articles on solving practical problems in large and medium-sized applications. In this article, PHP veteran Amol Hatwar discusses how to effectively use variables. He also demonstrated how to use variable names in PHP to construct a configuration file parser to simplify script configuration. In my previous article, I studied some of the factors that must be taken into account during planning, design, and even coding. In this article, you will be truly exposed to the actual code and will be able to see some things that are actually running. If you have not read the previous article, you 'd better take a look at it now. Correct processing of variable variables and functions is essential to any computer language. With variables, you can abstract data. with functions, you can abstract several lines of code. As Bruce Eckel said in his book C ++ programming ideas, all programming languages provide abstraction. Assembly language is a small abstraction of the underlying machine. Many of the following so-called imperative languages (such as Fortran, BASIC, and C) are abstract assembly languages. The types and quality of abstraction provided by programming languages are directly related to the complexity of the problems you can solve. Understanding how PHP processes variables and functions will help you use them effectively. What is in the name? As I mentioned in the previous article, naming conventions and coding conventions are important. No matter what naming conventions you use, remember to strictly abide by them in the project. If you use the most widely used naming conventions, your code will be accepted by more people. When naming variables, pay special attention not to overwrite the variables in use when including scripts. This is the root cause of common errors when new features are added to large applications. The best way to prevent this problem is to use a prefix. Use the abbreviated name of the module where the variable is located as the prefix. For example, if a module for voting processing contains a variable that saves the user ID, you can name the variable $ poll_userID or $ pollUserID. PHP is an interpreted language. This has many benefits, and soon you will learn to use some of them. The first obvious benefit is that it saves you the design-encoding-compilation-test cycle-any code you write in the editor is ready for use immediately. However, the most important benefit is that you don't have to worry about the variable type and how to manage these variables in memory. All scripts allocated to the script are automatically withdrawn by PHP after the script is executed. In addition, you can perform many operations on the variable without having to know the type of the variable. The code in listing 1 works normally in PHP, but a lot of error messages are thrown in C and Java: Listing 1. Sample PHP code with variables After installing PHP, if you want to run the code, you can first save the code as a. php file, then place the file on the Web server, and then point the browser to the file. A better solution is to install the CGI version of PHP. Then, enter the following command in a shell or command prompt and replace script-name with the file name containing your script to run the script. Path-to-php/php script-name: the code works properly, because PHP is a loose language. In easy-to-understand English, you can assign strings to integers without considering the variable type, and replace smaller strings with larger strings effortlessly. This is impossible in a language like C. Internally, PHP stores the data and types of variables separately. Type is stored in a separate table. Whenever an expression contains different types, PHP automatically determines what the programmer wants to do, changes the type in the table, and automatically evaluates the expression. To introduce a common small problem, you don't have to worry about the type, but sometimes it will make you into real trouble. What's going on? Here is an actual example: I often have to move the content created on a Windows-based PC to a Linux system so that they can be used on the Web. Windows-based file systems are case insensitive when processing file names. The file names DefParser. php and defparser. php point to the same file on Windows. In Linux, they point to different files. You may recommend that the file name be either in uppercase or lowercase, but the best practice is to keep the case unchanged. To solve this problem, we assume that you want a function that can check whether a given file exists in a directory regardless of case sensitivity. First, the task is divided into some simple steps. Code decomposition may sound a bit ridiculous, but it does help you focus on this code when writing it. In addition, the rewriting steps on paper are always much easier to write code: retrieve all file names in the source directory and filter them out. and .. check whether the target file exists in this directory. if the file exists, obtain the file name with the correct case. if the name does not match, false is returned to read the contents of the Directory. readdir () is required () function. You can get more details about the function in the PHP Manual (see references. As for the time being, as long as you know: readdir () will return the names of all files in the given directory one by one during each call. After listing all the file names, it returns false. You will use a loop that terminates when readdir () returns false. But is that enough? Keep in mind that PHP is a loose language, which means that the values of integer 0 and false are treated as the same (even C regards 0 and Boolean false as the equivalent ). The problem is not whether the code works normally. imagine what if the file name is 0! The script will be terminated too early. You can use the following script (listing 2) to determine the equivalence between 0 and Boolean false: Listing 2. determine whether 0 and Boolean false are equivalent. So what can you do? You know that PHP will store types internally, and if you can access these types, the problem will be solved. The Boolean value false is significantly different from the integer value 0. PHP has a gettype () function, but let's select a simpler method here. You can use the ===operator (yes, there are three equal signs ). The difference is that this operator compares the values and types of data at the same time. If you are confused about this, PHP still has it! = Operator. Only PHP 4 has these new operators and gettype () functions. Listing 3 shows the complete code to solve the problem: Listing 3. complete code I do not intend to describe the functions of each function in listing 3, but I encourage you to refer to the PHP Manual (see references ). When you use an unfamiliar function, the type of the parameter and return value is another error source. I did not describe the built-in functions in PHP, but intended to describe something that is not very clear at a glance. When different variable types are involved in the termination condition, use = and! = It is very important to perform a strong type check for operators. I can compile the entire script into a function, but here I divide the code into two functions. Do you still remember the rule "divide and conquer" in the previous article? I am doing this because each function plays a different role. If you use other scripts to obtain the content of a directory, you can now use it easily. I want you to consider some things: imagine implementing the entire script as a function, and then imagining the work required to debug, test, and reuse code. Use the loop correctly. now let's look at the foreach loop. why not use the for loop? Using the for loop requires you to know the number of items in the array-an additional step is required. In addition, PHP arrays may be processed beyond the array boundary. That is to say, when the array has only 10 elements, it tries to access its 15th elements. PHP does give a small warning, but as far as I know, in some cases, when a script is repeatedly run, the CPU activity rate suddenly increases to 100%, and the server performance drops continuously. We recommend that you avoid using the for loop whenever possible. At the end of assertion if, I want you to study the large if condition used in the get_file_list () function to ignore the large if conditions in the... directory. Obviously, I can use a traditional method to check variables based on constants. However, in many of my own code tricks, I often miss the equal sign and cannot find any problem in the future. Of course, PHP will not report an error because it thinks that I want to assign a value rather than compare it. When you compare constants based on variables and omit an equal sign, PHP will throw an error message. Variable names can now be used to discuss some amazing things. As a novice developer, it is confusing to use variable variables to complete a task, so it is often avoided. In fact, variable variables are easy to understand and use. They have helped me out of trouble more than once, and they are an important language element. In fact, in some cases, it is inevitable to use variable variables. Soon I'll look at this kind of reality, but first let's see what the variable is. Let's first try the code in listing 4: Listing 4. code with variable variables First, the code in listing 4 declares the variable $ myStr and assigns it the string I. The following statement defines another variable. But this time, the variable name is the data in $ myStr. $ MyStr is a method that tells PHP to generate another variable. it means "I want a variable and you can find the variable name in the variable $ myStr ". To do this, $ myStr must be defined. So now you have a variable named I and assign it a value using the string am. The following statement has done the same
Development of robust code in PHP "is a series of articles on solving practical problems in large and medium-sized applications. In this article, PHP veteran Amol Hatwar discusses how to effectively use variables ....