PHP (Hypertext Preprocessor) is a server-side scripting language that creates dynamic web pages.
In 1995, Rasmus Lerdorf created PHP while looking for a way to keep track of visitors coming into his website. He wrote Common Gateway Interface (CGI) scripts in C, known as “Personal Home Page tools” (or PHP tools), that allowed him to render web pages and gather information dynamically.
In 1997, Andi Gutmans and Zeev Suraski rewrote the language’s core and introduced PHP/FI 2.0, which was faster and more flexible than previous versions of PHP. It continued to evolve and expand with new features. In the late 1990s and early 2000s, it was widely used to build dynamic webs.
Today, PHP is one of the world’s most widely used
PHP processes the code on the server side before sending the results to the client, allowing it to perform complex operations like database connectivity, mathematical calculations, and other dynamic tasks. This would not be possible with client-side languages such as HTML, CSS, or JavaScript. Here’s a flowchart to illustrate how PHP works:
Note: PHP code is processed on the server, and the client never receives or sees that code. Instead, it only gets the code-generated output.
There are numerous programming languages available in the market. This section will discuss why we’d use PHP. Some of the fundamental advantages are as follows:
Easy to learn: PHP is a reasonably simple scripting language, especially for those familiar with other programming languages. Several resources (online courses, forums, and books) are also available to assist beginners in learning the language and developing scripts using it.
Open-source: PHP is an open-source language, which means it is free to use and has a significant and active developer community that assures its ongoing development and maintenance.
Server-side scripting: Being a server-side scripting language, PHP processes the code on the server side before sending the results to the client, allowing it to perform complex operations like database connectivity, mathematical calculations, and other dynamic tasks.
Cross-platform compatibility: PHP can operate on various operating systems like Mac, Windows, and Linux, making it a versatile and adaptable choice for web programming.
Here’s a typical “Hello, World!” example of a PHP script:
<!DOCTYPE html> <html> <head> <title>Hello, World!</title> </head> <body> <?php echo "Hello, World!"; ?> </body> </html>
Note: A PHP file contains PHP tags and ends with the extension
.php
.
Line 1: The declaration <!DOCTYPE html>
indicates that the page is written in HTML5.
Line 2: The <html>
tag defines the beginning of the HTML document.
Lines 3–5: The <head>
section contains the meta-info of the document, such as the title of the page within the <title>
tag, which will be displayed in the browser’s title bar.
Lines 6–10: The <body>
section contains the main content of the page.
Lines 7–9: The <?php
and ?>
tags define a block of PHP code.
Line 8: The echo
statement outputs the string "Hello, World!"
to the page.
Line 11: The </html>
tag defines the end of the HTML document.