Develop robust code with PHP: effectively use variables

Source: Internet
Author: User
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 BruceEckel 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. Programming Correct variable processing
Variables and functions are 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.
  
   Understanding PHP variables
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
  
   $ MyStr = 789696; // An integer.
$ MyVar = 2; // Another integer.
$ MyStr = "This is my favorite band:"; // Strings are more fun.
$ MyStr = $ myStr. "U". $ myVar; // Doing this is OK, too.
Echo "$ myVar \ n ";
?>
  
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
  
This 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.
  
   This section describes a common small problem.
There is no need 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.
  
   Solve this small problem
Suppose you want a function that can check whether a given file exists in a directory without 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, rewriting steps on paper are always much easier to write code:

12 3 4 Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.