Developing robust code in PHP: using variables efficiently

Source: Internet
Author: User

   Handle variables correctly
Variables and functions are essential elements of any computer language. With variables, you can abstract the data, and with functions, you can abstract a few lines of code. As Bruce Eckel in his book, "C + + programming ideas", all programming languages provide abstractions. Assembly language is a small abstraction of the underlying machine. Many of the subsequent so-called imperative languages (such as Fortran, BASIC, and C) are abstractions of assembly language.
  
The kind and quality of abstraction provided by programming languages is directly related to the complexity of the problems you can solve. Understanding how PHP handles variables and functions will help you to use them effectively.
  
   What's in the name?
As I mentioned in the previous article, naming conventions and coding conventions are important. Regardless of the naming convention you use, remember to strictly follow it in your project. If you use the most widely used naming convention, your code will be accepted by more people.
  
When you name a variable, be careful not to overwrite the variable being used when you include the script. In large applications, this is a common source of error when adding new functionality. The best way to prevent this problem is to use a prefix. Use the name abbreviation of the module that contains the variable as a prefix. For example, if a module that handles voting has a variable that holds the user's identity, you can name the variable $poll _userid or $pollUserID.
  
   Understanding PHP Variables
PHP is an interpreted language. There are many benefits, and soon you will learn to take advantage of some of them. The first obvious benefit is that it saves you from designing-coding-compiling-testing cycles-Any code you write in the editor is immediately available. However, the most important benefit is that you don't have to worry about the types of variables and how to manage them in memory. All memory allocated to the script is automatically retracted by PHP after the script is executed. In addition, you can perform many operations on a variable without knowing the type of the variable. The code in Listing 1 works perfectly well in PHP, but throws a lot of error messages in the C and Java languages:
  
Listing 1. Sample PHP code with variables
  
   $MYSTR = 789696; An integer.
$myVar = 2; Another integer.
$MYSTR = "This is my favorite band:"; Strings is more fun.
$MYSTR = $myStr. "U".    $myVar; Doing This is OK, too.
echo "$myVar \ n";
?>
  
After you install PHP, to run the running code, you can first save the code as a. PHP file, place the file on the Web server, and then point the browser to the file. A better approach is to install the CGI version of PHP. Then, you can run the script by entering the following command at the shell or command prompt and replacing script-name with the file name that contains your script.
  
path-to-php/php Script-name
  
The code works correctly because PHP is a loose-type language. In easy-to-understand English, you can assign a string to an integer without taking into account the variable type, and replace the smaller string with a larger string effortlessly. This is impossible in a language like C. Internally, PHP stores the data owned by the variable separately from the type. The type is stored in a separate table. Whenever an expression with different types appears, PHP automatically determines what the programmer wants to do, changes the type in the table, and evaluates the expression automatically.
  
   introduce a common small problem
It's nice not to worry about types, but sometimes that can get you into real trouble. What's going on here? Here's a practical example: I often have to move content created on a Windows-based PC to a Linux system so that it can be used on the Web. The Windows-based file system is case-insensitive when it processes filenames. File names defparser.php and defparser.php point to the same file on Windows. On the Linux operating system, they point to different files. You might advocate that the file name is either all uppercase or lowercase, but the best practice should be to keep the case intact.
  
   solve this little problem.
Suppose you want a function that checks whether a given file exists in a directory without regard to case. First, break this task down into a few simple steps. Breaking down your code may sound ridiculous, but it does help you focus on this code as you write your code. Also, rewriting steps on paper is always a lot easier than writing 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.