What is PHP?
PHP is an acronym for PHP: Hypertext Preprocessor. It is an open-source, HTML-embedded server-side scripting language used primarily for creating dynamic and interactive web applications.
PHP code is executed on the server, and the result is returned to the browser as plain HTML. The user never sees the original PHP source code.
Key Facts About PHP
- Server-Side: Unlike client-side languages (like JavaScript), PHP code runs on the web server (e.g., Apache, Nginx) before the page is sent to the user’s browser.
- Open-Source & Free: It is completely free to download, use, and modify.
- Embedded in HTML: PHP scripts are typically embedded within the normal HTML code of a web page.
- Database Connectivity: PHP is easily integrated with many popular databases, most notably MySQL, making it ideal for data-driven web applications.
- Origin: PHP was originally created by Rasmus Lerdorf in 1994.
Why Use PHP? (What You Can Do)
PHP is one of the most popular web development languages in the world, powering major platforms such as WordPress, Facebook, and Wikipedia.
With PHP, you can:
- Handle Forms: Collect form data, save it to a database, or send it via email.
- Generate Dynamic Content: Create, open, read, write, and close files on the server.
- Database Interaction: Add, delete, modify, and retrieve data from a database (like MySQL).
- Manage Sessions & Cookies: Track user activity and store user-specific information.
- Restrict Access: Control which users can view specific pages of your website.
📝 Your First PHP Script
To get a feel for how PHP works, here is the traditional “Hello, World!” script. Remember, PHP code must be placed within special PHP tags.
Example: Hello World
The following code mixes HTML structure with a single line of PHP:
PHP
<!DOCTYPE html>
<html>
<head>
<title>Hello PHP</title>
</head>
<body>
<h1>PHP Output Example</h1>
<?php
echo "Hello, World! I am running on the server.";
?>
</body>
</html>
Explanation of the Code
| Line | Element | Description |
<?php | Opening Tag | This tag tells the server’s PHP processor where the PHP code begins. |
echo "..." | Command | The echo statement is used to output text, variables, or HTML directly to the browser. |
; | Terminator | A semicolon marks the end of a PHP statement (similar to C, Java, etc.). This is mandatory. |
?> | Closing Tag | This tag tells the server where the PHP code ends. |
Next Steps
In the next chapter, we will show you how to set up your local development environment (using XAMPP or MAMP) so you can start running these PHP scripts yourself!
