PHP Is a Programming Language

Programming languages are means for describing tasks to be done by a computer, in terms that humans can readily understand. Computers, being digital devices, communicate and manipulate information (including instructions) in the form of numbers. The first computers' operators inputted a series of numbers into the machines and received numbers as output. Since humans think in terms of language, every command had to be translated from an idea expressible in words (such as, add the next two numbers together) into a number representing that particular command. As you can imagine, this was highly tedious. So, early in the development of computers came the development of languages for programming computers.

Some elements of languages include symbols, such as the + sign. Others contain actual words, such as print. Others include pseudo-English, such as fprintf, which in the C programming language means, print something to a file (the first f) in a particular format (the second f). Here is an example of a function in the C programming language which marks the pixels in an image that fall within certain color ranges (in particular, this is part of a face-detection program):

void MarkSkin( rgbimage *image, int marker ) {

  int i, j;

  for ( i = 0 ; i < image->ydim; i++ )
    for ( j = 0; j < image->xdim; j++ ) {
      if ( ( image->r[i][j] >= MIN_RED && image->r[i][j] <= MAX_RED ) &&
        ( image->g[i][j] >= MIN_GREEN && image->g[i][j] <= MAX_GREEN ) &&
        ( image->b[i][j] >= MIN_BLUE && image->b[i][j] <= MAX_BLUE ))
        image->r[i][j] = image->g[i][j] = image->b[i][j] = marker;
    }
}

(Basically, it tells the computer to go through each row and column of the image, examine the red, green, and blue values of the each pixel, and, if they fall within certain ranges, then set them to a marker value.)

You have probably heard the names of some computer languages. Common languages include Assembly, BASIC, C, FORTRAN, Java, LISP, Pascal, Prolog, and Visual C++. There are many more. Why are there so many languages? The reason is that different people designed the different languages, often with a particular purpose in mind. Accordingly, some languages are more efficient than others for directing the computer to do certain kinds of tasks. Also, as hardware has gotten faster and memory cheaper, computer languages have adapted. Finally, computer-science theorists tweak and redesign languages to make them better able to express complex ideas and more efficiently communicate tasks to be performed.

Does an ordinary computer user need to know a computer language? Perhaps not, but it does lead to a deeper understanding of how computers work, and knowledge of PHP in particular may become valuable as more and more businesses and individuals express themselves through web pages.

Computer languages are classified into several categories:

    1. Functional languages work like mathematical functions (remember f(x) = 2x?). In a functional language, a function describes how input is converted to output.

    2.  
    3. Imperative languages work like a drill sergeant: Do this. Do that. Input a filename from the user. Now print out that filename. Get down and give me 50 . . . .

    4. Declarative languages emphasize the statement of the problem. In a declarative language, the programmer creates a general algorithm, for instance, an economic simulation. Additional code consists of stating specific problems in terms of the general algorithm (e.g., determining tax revenues might be "declared" by relating it to personal incomes and tax rates).

    5. Object-oriented languages create virtual objects, like a window or a document, and then communicate with the objects through interfaces. For instance, if you had a dog object called Seamus, you might tell him to sit by saying Seamus.sit(). The point of the objects is to modularize and simplify the programming. Once you have created a dog named Seamus, you don't need to worry about his physiology (is his kneebone connected to his . . . thighbone?). All you need to do is give him commands.

PHP, as we will see, combines elements of functional and imperative languages.

When a programmer writes something in a computer language, the resulting text document is called source code. However, a computer cannot understand text, but only numbers. Accordingly, the source code must be translated into commands that a computer processor can understand. The translation is done by one of two types of computer programs:

    1. Interpreters take source code and directly execute it. The source code is treated as a list of instructions to the interpreter, which carries out the instructions. When handling interpreted code, you must run the interpreter program, which adds significant overhead, especially if the source code is small.

    2. Compilers convert source code into executable code that can be run at a later time. Unlike an interpreter, you only run a compiler once, and, thereafter, you run the executable code (also called the binary, because binary numbers represent instructions for the computer processor). Also, because a compiler works on the entire source code at once, it can optimize the resulting executable code. Finally, executable code is very hard to reverse-engineer, that is, given a piece of executable code, it is very difficult to figure out what the corresponding source code was. This is a benefit for software companies who want to protect their trade secrets.

Most programs written for Windows, the Macintosh operating system, or UNIX, are distributed as executable code. For example, the program files for Microsoft Word or Adobe Photoshop consist of binary numbers: CPU instructions and related data. Thus, you don't have to run an interpreter in addition to the program you want to run. Moreover, it would be very difficult to figure out how Word was programmed by examining the executable code, so Microsoft's trade secrets are secure.

PHP programs are interpreted. In the case of PHP, the interpreter is part of the web server. Since the web server is running already, there isn't a lot of additional overhead in interpreting PHP. Moreover, PHP programs tend to be relatively short, so the kind of optimization that compilers do would be unlikely to improve performance significantly. Also, not having to recompile each time you make a small change to a PHP program makes it much easier to maintain a web site.

The Open Source Software Movement promotes supplying source code along with executable programs. Accordingly, Linux, and programs that run on Linux, are distributed with their source code. This means that a sophisticated user can tweak the code and recompile the operating system and applications to customize them. Distribution of source code also has the benefit in that the code is made public, and mistakes in the code are usually quickly identified. This is one of the main reasons Linux is considered to be more robust than Windows.

Next (The Echo Command)