How do you start a PHP script?
1. The Basic Syntax: The PHP Tags
A PHP script starts with the <?php opening tag and ends with the ?> closing tag. Any code between these tags is interpreted as PHP.
Basic Structure:
<?php
// This is where your PHP code goes
?>
<?php: This is the standard and recommended way to start a PHP script. It's guaranteed to work on every server configuration.?>: This is the closing tag. If a file contains only PHP code, it is actually recommended to omit the closing tag to avoid unwanted whitespace or newline characters causing issues.
2. Your First Simple Script: "Hello, World!"
Let's create a basic script that outputs text.
Example: hello.php
<?php
echo "Hello, World!";
?>
Explanation:
echois a language construct used to output (print) one or more strings.- The line ends with a semicolon (
;). In PHP, almost every line of code that is a statement must end with a semicolon.
3. How to Run a PHP Script (The Crucial Part)
Unlike HTML files, which you can simply open in a browser, PHP files need to be executed by a PHP interpreter on a web server. You cannot run them by double-clicking the file.
You have three main options to run a PHP script:
Option A: Using a Local Development Server (Most Common for Learning)
This is the best way to learn and test. You install a software stack on your own computer that includes a web server (like Apache or Nginx) and the PHP interpreter.
-
Install a Package:
- Windows: XAMPP, WampServer, or Laragon.
- macOS: XAMPP, MAMP, or use the built-in PHP with Homebrew.
- Linux: Use your package manager (e.g.,
sudo apt install apache2 php mysql-server).
-
Start the Servers: Open the control panel (e.g., XAMPP) and start the "Apache" web server.
-
Place Your File: Put your PHP file (e.g.,
hello.php) inside the "web root" directory. This is typically:- XAMPP:
C:\xampp\htdocs\(Windows) or/Applications/XAMPP/htdocs/(macOS) - WampServer:
C:\wamp64\www\
- XAMPP:
-
Run in Browser: Open your web browser and go to:
http://localhost/hello.php
The browser will display: Hello, World!
Option B: Using PHP's Built-in Development Server (Quick & Easy)
For simple testing, PHP has a built-in web server. This is not suitable for production but is perfect for quick experiments.
- Open a Terminal or Command Prompt.
- Navigate to the directory where your
hello.phpfile is located.cd /path/to/your/php/script - Start the server on a specific port (e.g., 8000):
php -S localhost:8000 - Run in Browser: Go to
http://localhost:8000/hello.php
Option C: On a Live Web Hosting Server (For a Real Website)
When you want to put your website online, you upload your .php files to a web hosting provider that supports PHP.
- Upload
hello.phpto your public directory (often calledpublic_htmlorhtdocs) using an FTP client or file manager. - Access it via your domain name:
http://yourwebsite.com/hello.php
4. Key Ways to "Start" or Trigger Output Within a Script
Once your script is running, you need ways to produce output and execute logic.
| Method | Description | Example |
|---|---|---|
echo / print |
Outputs strings or variables. echo can take multiple parameters; print can only take one. |
echo "Hello, ", $name; |
| HTML outside PHP tags | You can write HTML directly, and use PHP tags for the dynamic parts. | (See example below) |
| Variables | Start with a $ sign. |
$message = "Welcome!"; |
Conditionals (if) |
Execute code based on conditions. | (See example below) |
Practical Example: Mixing HTML and PHP
A PHP file can be mostly HTML, with PHP snippets inside it to generate dynamic content. This is very common.
File: welcome.php
<!DOCTYPE html>
<html>
<head>
<title>My PHP Page</title>
</head>
<body>
<h1>Welcome to my Site</h1>
<!-- This is pure HTML -->
<p>This is a static paragraph.</p>
<?php
// This is a PHP code block
$userName = "Alice";
$hour = date('H'); // Gets the current hour (0-23)
// Use an if/else statement to change the message
if ($hour < 12) {
$greeting = "Good morning";
} else {
$greeting = "Good afternoon";
}
// Output the dynamic greeting
echo "<p>$greeting, $userName!</p>";
?>
<p>This is more static HTML.</p>
</body>
</html>
Expected Output in Browser:
Welcome to my Site
This is a static paragraph.
Good afternoon, Alice! (This will change based on the time of day)
This is more static HTML.
Summary: How to Start
- Write your code inside
<?php ... ?>tags. - Save the file with a
.phpextension. - Run it on a server (local like XAMPP, the built-in PHP server, or a live web host).
- View the output in your web browser by visiting the script's URL.
The most important takeaway is that PHP is a server-side technology. You must always access your .php files through a URL (http://...) that is handled by a web server with PHP enabled, not by opening the file directly from your file system (file://...).
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
