If you are interested in developing web pages, you must have surely heard of PHP. The acronym stands for Hypertext Processor, and it is an open-source web programming language similar to HTML. The difference is that PHP can render web pages dynamically. The scripts run on the server side, and the results are seen on the browser side. Here are some PHP basics for your benefit.
What You Can Do with PHP
- You can create large, complicated websites quickly and easily.
- You can download readymade PHP scripts, customize and use them.
- You can display a customized output to each viewer, based on their input.
How Does the PHP Script Work?
When someone opens your webpage, they see text and picture content. The mathematical calculations and file operations executed within the PHP script remain hidden, even if the viewer opens the web page’s source code. This is because PHP commands are only visible to the server, and not the browser.
Salient Features of a PHP Script
If you are familiar with programming languages like Java, Perl or C, it is very easy to learn the PHP basics.
Script Start and End
Every PHP script begins and ends with , and is saved with a .php extension. Your script could be standalone, but if it is embedded into HTML code, this extension indicates to the server to first process and execute the PHP script before rendering the HTML file as a webpage to the browser.
You might need to define variables before you begin coding. All variables in PHP are preceded with a dollar sign. The name can begin with an alphabet or underscore, and is case-sensitive. You cannot use reserved keywords such as $this or $_server.
- ECHO function: This enables you to output text on the viewer’s browser.
- Conditional IF and SWITCH functions: Both are similar, but the latter has a higher number of possible outcomes.
- FOR and WHILE loops: These allow repeated processing with incrementing / decrementing variable values. PHP supports nested looping, but an overly extensive script can slow down the overall execution, thus nullifying the main advantage of PHP.
There are a large number of ready-to-use PHP functions, but you can also define your own. These are useful for repeated processing that uses variable input data each time. When defining a function, you need to give it a name, list of parameters, and the formula statement.
It is a good habit to include comments in your PHP script, like with any other programming language. These lines are not executed by the server; they are merely there for you or someone else to understand the flow at a later date, for ease of debugging or editing. Comment lines in PHP are preceded by the double back slash “//”.
Testing of PHP Script
You will need a database to test the output of any PHP script that you develop. The most common database used with PHP is MySQL. Alternatively, you can pay for web hosting; in this case, your PHP test file is uploaded to the actual website where it needs to execute. Most websites have PHP installed these days, and are enabled to run these scripts.