Considering the Various Uses for the PHP Scripting Language - dummies

Considering the Various Uses for the PHP Scripting Language

By Janet Valade

PHP is a general-purpose language that can be used to write general-purpose scripts. Scripts are computer files containing instructions in the PHP language that tell the computer to do things, such as display Hello on the screen or store some specified data in a database. Most scripts contain a series of instructions that can accomplish tasks from designing Web pages to navigating your file system. Because PHP began life on the Web, it has many features that are particularly well suited for use in scripts that create dynamic Web pages. Currently, you find PHP most often hard at work in Web pages, but its use for other purposes is growing.

PHP is very popular for Web sites. According to the folks at PHP, over 11 million domains are using PHP. Yahoo!, which is among the world’s most visited sites, recently decided to change from its own proprietary language to PHP.

Using PHP for Web applications

PHP is a server-side scripting language, which means that the scripts are executed on the server (the computer where the Web site is located). This is different than JavaScript, another popular language for dynamic Web sites. The browser, on the user’s computer, executes JavaScript. Thus, JavaScript is a client-side language.

Because PHP scripts execute on the server, PHP can dynamically create the HTML code that generates the Web page, which allows individual users to see customized Web pages. Web page visitors see the output from scripts, but not the scripts themselves.

PHP has many features designed specifically for use in Web sites, including the following:

  • Interact with HTML forms: PHP can display an HTML form and process the information that the user types in.
  • Communicate with databases: PHP can interact with databases to store information from the user or retrieve information that is displayed to the user.
  • Generate secure Web pages: PHP allows the developer to create secure Web pages that require users to enter a valid username and password before seeing the Web page content.

PHP features make these and many other Web page tasks easy.

PHP is only server-side, meaning it can’t interact directly with the user’s computer. That means PHP can’t initiate actions based on the status of the user’s computer, such as mouse actions or screen size. Therefore, PHP alone can’t produce some popular effects, such as navigation menus that drop down or change color. On the other hand, JavaScript, a client-side scripting language, can’t access the server, limiting its possibilities. For example, you can’t use JavaScript to store data on the server or retrieve data from the server. But wait! You don’t have to choose. You can use JavaScript and PHP together to produce Web pages that neither can produce alone.

Using PHP for database applications

PHP is particularly strong in its ability to interact with databases. PHP supports pretty much every database you’ve ever heard of and some you haven’t. PHP handles connecting to the database and communicating with it, so you don’t need to know the technical details for connecting to a database or for exchanging messages with it. You tell PHP the name of the database and where it is, and PHP handles the details. It connects to the database, passes your instructions to the database, and returns the database response to you.

Major databases currently supported by PHP include the following:

  • dBASE
  • Informix
  • Ingres
  • Microsoft SQL Server
  • mSQL
  • MySQL
  • Oracle
  • PostgreSQL
  • Sybase

PHP supports other databases, as well, such as filePro, FrontBase, and InterBase. In addition, PHP supports ODBC (Open Database Connectivity), a standard that allows you to communicate with even more databases, such as Access and IBM DB2.

PHP works well for a database-driven Web site. PHP scripts in the Web site can store data in and retrieve data from any supported database. PHP also can interact with supported databases outside a Web environment. Database use is one of PHP’s best features.

Using PHP with your file system

PHP can interact with your file system — the directories and files that are on your local hard disk or on other computers accessible over a network. PHP can write into a file on your file system, creating the file if it doesn’t exist, and can read the contents from files. It can also create directories, copy files, rename files, delete files, change file attributes, and perform many other file system tasks. PHP allows you to perform almost any task related to your file system.

Many Web sites need to interact directly with the file system. For example, a Web application may save information temporarily in a file rather than in a database, or may need to read information from a file.

System administrative and maintenance scripts frequently need to interact with the file system. For example, you may want to use a PHP script to back up files, to clean out directories, or to process text files by reformatting their contents. PHP can perform these tasks quite well.

Using PHP for system commands

PHP can interact with your operating system to perform any task the operating system can perform. You can execute an operating system command and receive the output. For example, you can execute a dir or ls command (to list the files in your directory) from PHP and receive the list of filenames that the dir/ls command produces.

The ability to execute system commands is often useful for system administrative and maintenance tasks. For example, you may want to clean up a directory by deleting files with a particular extension. You can use a system command to get a list of files in a directory and then identify and delete the files with the unwanted extension.

The ability to execute system commands includes the ability to run any other program on the system. Thus, you can run programs in other languages from PHP and make use of the output. Aren’t you relieved that you don’t have to rewrite all those programs you’re using now? You can run Perl, C, shell scripts, or any other language programs from PHP. New PHP programs can add functionality to your system tools, without requiring you to spend time rewriting existing tools.