Develop robust code with PHP: Use variables effectively, part 2nd

Source: Internet
Author: User
Tags abstract expression install php integer naming convention php file php code

"Developing robust code in PHP" is a series of articles about solving practical problems in large and medium applications. In this article, the PHP veteran Amol Hatwar discusses how to use variables effectively. He also demonstrates how to construct a configuration file parser by using variable names in PHP to simplify script configuration.

In my previous article, I studied some of the factors that must be considered during planning, designing, and even writing code. In this article, you'll really get to the real code and see what's actually running. If you haven't read the previous article, it's best to take a look at it now.

Handling variables correctly

Variables and functions are essential elements of any computer language. With variables, you can abstract data and, with functions, you can abstract a few lines of code. As Bruce Eckel in his book The idea of C + + programming, 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 type and quality of abstraction provided by the programming language is directly related to the complexity of the problems you can solve. Understanding how PHP handles variables and functions will help you use them effectively.

What's in the name?

As I mentioned in the previous article, naming conventions and coding conventions are important. No matter what 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 naming variables, be particularly careful when including scripts to not overwrite variables that are in use. 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 initials of the module that contains the variable as a prefix. For example, if a module that processes 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 do not have to worry about the types of variables and how to manage them in memory. All memory assigned to the script is automatically retracted by PHP after the script is executed. In addition, you can perform many operations on a variable without having to know 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

<?php
$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 you install PHP, to run the 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 would be to install the CGI version of PHP. Then, you can run the script by entering the following command at the shell or at the command prompt, replacing script-name with the filename that contains your script.

path-to-php/php
script-name

The code works fine because PHP is a loosely typed language. In plain English, you can assign a string to an integer without considering the variable type, and effortlessly replace the smaller string with a larger string. This is impossible in a language like C. Internally, PHP stores the data that the variables have with the type separately. Types are stored in separate tables. Whenever a different type of expression appears, PHP automatically determines what the programmer wants to do, changes the type in the table, and then automatically values the expression.

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.