PHP is one of the most widely used server-side scripting languages in the world. It powers millions of websites, from small blogs to large-scale applications like Facebook (in its early days) and WordPress. If you’re new to web development, PHP is a great starting point because it’s easy to learn yet powerful enough to build complex applications.
What is PHP?
PHP stands for Hypertext Preprocessor. It is a server-side scripting language designed to build dynamic and interactive web applications.
Key characteristics of PHP include:
- Server-side execution: Code runs on the server, and the result (usually HTML) is sent to the browser.
- Cross-platform: Runs on Windows, Linux, and macOS.
- Open-source: Free to use with a strong global community.
- Database integration: Works easily with databases like MySQL, PostgreSQL, and MariaDB.
- Embedding in HTML: You can write PHP directly inside HTML files.
Setting Up PHP
Before writing PHP, you need to set up an environment. There are several approaches:
Option 1: Local Server Packages (Beginner Friendly)
Tools like XAMPP, WAMP, MAMP, or Laragon provide everything you need:
- Apache (web server)
- PHP (scripting language)
- MySQL (database)
Steps (using XAMPP as an example):
- Download XAMPP.
- Install it on your system.
- Start Apache (for PHP) and MySQL (for databases).
- Place your
.phpfiles in thehtdocsfolder.
Visit http://localhost/filename.php in your browser.
Option 2: Install PHP Manually
For developers who want more control:
- Download PHP from php.net.
- Add PHP to your system’s PATH.
- Run PHP scripts from the terminal:
php filename.php
Option 3: Online PHP Sandboxes
Websites like 3v4l.org or PHP Fiddle allow you to test PHP code without installing anything.
Writing Your First PHP Script
Create a file named index.php in your server’s root directory (htdocs for XAMPP).
<?php
echo "Hello, World!";
?>
Now visit:
http://localhost/index.php
You should see:
Hello, World!
This is the classic beginner’s script, confirming that PHP is working.
PHP Embedded in HTML
One of PHP’s strengths is mixing logic with HTML. Example:
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<h1><?php echo "Welcome to PHP!"; ?></h1>
<p>Today’s date is: <?php echo date("Y-m-d"); ?></p>
<p>The time now is: <?php echo date("H:i:s"); ?></p>
</body>
</html>
Output in the browser:
- Displays a dynamic heading.
- Shows today’s date and current time.
Understanding PHP Syntax
PHP code is written inside <?php ... ?> tags.
Basic Rules:
- Every statement ends with a semicolon (
;). - Whitespace doesn’t matter but improves readability.
- PHP code can be mixed with HTML.
Example:
<?php
echo "Line 1";
echo "Line 2";
?>
Comments in PHP
Comments are ignored by the interpreter but helpful for developers.
<?php
// This is a single-line comment
# Another single-line comment
/*
This is a multi-line comment.
Useful for long explanations.
*/
?>
PHP Execution Flow
- User requests a PHP page (
index.php). - The server processes PHP code.
- PHP generates HTML (or JSON, XML, etc.).
- The server sends the result to the browser.
Example:
index.php
<?php
echo "<h1>Hello, User!</h1>";
?>
Browser receives only:
<h1>Hello, User!</h1>
The PHP code itself is never visible to the browser.
Why Learn PHP Today?
Even with new frameworks and languages, PHP remains relevant because:
- It powers WordPress, Drupal, Joomla, Magento, and more.
- It integrates easily with databases and APIs.
- Hosting for PHP is widely available and affordable.
- Frameworks like Laravel make building apps faster and more secure.
Conclusion
In this first post, we covered:
- What PHP is and why it’s important.
- How to set up PHP (local server, manual install, or online sandbox).
- Writing and running your first script.
- Embedding PHP in HTML.
- Basic PHP syntax and comments.
- How PHP works behind the scenes.
In the next post, we’ll dive into variables, data types, and operators — the building blocks of programming in PHP.