First PHP Program on Localhost: My First Program Tutorial
Running PHP on localhost requires a web server like XAMPP. This tutorial shows you how to write your first PHP script, save it in the htdocs folder, and view it in your browser at http://localhost.
First PHP Program on Localhost
Now that you have XAMPP installed and running, it is time to write and run your first PHP program. PHP is a server-side scripting language that allows you to create dynamic web pages. Unlike HTML, which is static, PHP code is executed on the server before the page is sent to your browser. This means you can create websites that respond to user input, interact with databases, and display personalized content.
The traditional first program in any programming language displays a message on the screen. In PHP, this is incredibly simple. You will create a PHP file, save it in the correct folder, and view it in your browser. To understand this tutorial properly, it is helpful to have XAMPP installed and running with Apache started.
✓ Understanding PHP and how it works on localhost
✓ Creating your first PHP file in the htdocs folder
✓ Writing PHP code inside HTML
✓ Using echo and print statements
✓ Viewing PHP output in your browser
✓ Understanding PHP configuration (phpinfo)
✓ Common PHP syntax and best practices
What Is PHP
PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language. It is especially suited for web development and can be embedded directly into HTML. PHP scripts are executed on the server, and the result is sent to the browser as plain HTML. This makes PHP powerful for creating dynamic content, handling form data, managing sessions, and interacting with databases.
- Server-Side: Code runs on the server, not in the browser.
- Embedded: PHP code can be mixed with HTML.
- Free and Open Source: No licensing costs.
- Database Friendly: Excellent support for MySQL, PostgreSQL, and more.
- Cross-Platform: Runs on Windows, macOS, Linux, and web servers.
- Large Community: Millions of developers use PHP worldwide.
Step 1: Locate the htdocs Folder
Before you can run PHP scripts, you need to know where to put your files. XAMPP uses the `htdocs` folder as the document root. Any file or folder you place inside `htdocs` becomes accessible through your browser.
Windows: C:\xampp\htdocs\
macOS: /Applications/XAMPP/htdocs/
Linux: /opt/lampp/htdocs/
Create a new folder for your projects:
C:\xampp\htdocs\first-program\
Step 2: Create Your First PHP File
Open any text editor (Notepad, VS Code, Sublime Text, or Brackets). PHP files have the extension `.php`. Create a new file called `index.php` inside your project folder.
<!DOCTYPE html>
<html>
<head>
<title>First PHP Program</title>
</head>
<body>
<h1>Welcome to PHP Programming</h1>
<?php
// This is first PHP code
echo "<p>First PHP program is running successfully!</p>";
echo "<p>Current date and time: " . date('Y-m-d H:i:s') . "</p>";
?>
</body>
</html>
Step 3: Understanding the PHP Tags
PHP code is enclosed in special tags that tell the server where PHP starts and ends. The most common tag is `<?php` to open and `?>` to close.
<?php
// All PHP code goes between these tags
echo "This is PHP code";
?>
<?php echo "Short tag (not recommended on all servers)"; ?>
<?= "Short echo tag (works in PHP 5.4+)" ?>
Step 4: Using echo and print
The `echo` and `print` statements are used to output data to the browser. They are the most common ways to display content from PHP.
<?php
// echo can output multiple strings
echo "Hello", " ", "World", "!";
// print outputs a single string and returns 1
print "Hello World";
// echo with HTML tags
echo "<h2>This is a heading</h2>";
echo "<p>This is a paragraph with <strong>bold</strong> text.</p>";
// echo with variables
$name = "John";
echo "Welcome, " . $name;
// Using double quotes for variable interpolation
echo "Welcome, $name";
?>
Step 5: Run Your First PHP Program
Now it is time to see your PHP program in action. Make sure Apache is started in XAMPP Control Panel, then open your browser.
1. Make sure Apache is running (green light in XAMPP)
2. Open your web browser
3. Type this address:
http://localhost/first-program/
or
http://localhost/first-program/index.php
4. Press Enter
5. You should see your PHP program output!
Step 6: View PHP Information
PHP comes with a built-in function called `phpinfo()` that displays all configuration information about your PHP installation. This is useful for debugging and understanding your environment.
1. Create a new file called "info.php" in your project folder
2. Add this code:
<?php
phpinfo();
?>
3. Access it at: http://localhost/first-program/info.php
4. You will see detailed PHP configuration including:
- PHP version
- Loaded extensions
- Server API
- Configuration file path
- Environment variables
Step 7: PHP Variables and Data Types
Variables in PHP start with a dollar sign (`$`) followed by the variable name. PHP is loosely typed, meaning you don't need to declare the data type explicitly.
<?php
// String
$name = "John Doe";
// Integer
$age = 25;
// Float (Decimal)
$price = 19.99;
// Boolean
$isActive = true;
// Array
$colors = array("Red", "Green", "Blue");
// Null
$nothing = null;
// Display variables
echo "Name: " . $name . "<br>";
echo "Age: $age<br>";
echo "Price: $$price<br>";
// Check variable type
var_dump($name);
?>
Step 8: Basic PHP Syntax Rules
| Rule | Description | Example |
|---|---|---|
| Variables start with $ | All variable names begin with a dollar sign | $name = "John"; |
| Statements end with semicolon | Every PHP statement must end with a semicolon | echo "Hello"; |
| Case-sensitive | Variable names are case-sensitive | $name vs $Name are different |
| Comments | Single line: // or #, Multi-line: /* */ | // This is a comment |
| Whitespace ignored | Spaces and line breaks are ignored | Indentation for readability |
Common PHP Errors and Solutions
| Error | Cause | Solution |
|---|---|---|
| Parse error: syntax error | Missing semicolon or incorrect syntax | Check for missing semicolons or brackets |
| Undefined variable | Using a variable that hasn't been set | Initialize variables before using them |
| Cannot modify header information | Output before header() call | Move header() before any HTML output |
| 404 Not Found | File not in correct location or Apache not running | Check file path and start Apache |
<?php
// Turn on error reporting during development
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Your code here
?>
What's Next
Congratulations! You have written and run your first PHP program. Now that PHP is working on your localhost, you can explore more advanced topics:
- PHP Variables and Data Types
- Handling HTML Forms with PHP
- Connecting PHP to MySQL Database
- Loops and Conditional Statements
- Creating Custom Functions
Frequently Asked Questions
- Why is PHP code showing as plain text in the browser?
This means Apache is not processing PHP files. Make sure Apache is running and the file has a `.php` extension, not `.html`. - What is the difference between echo and print?
`echo` is slightly faster and can take multiple parameters. `print` returns a value (1) and can only take one parameter. For simple output, both work fine. - Do I need to restart Apache after changing PHP files?
No, PHP files are interpreted on each request. You only need to restart Apache after changing configuration files like `php.ini`. - Where can I find the PHP error log?
XAMPP stores PHP error logs at: `C:\xampp\php\logs\php_error_log` (Windows) or `/Applications/XAMPP/logs/` (macOS). - How do I check PHP version?
Create a file with `<?php phpinfo(); ?>` or run `php -v` in the command line. - What should I learn next after first PHP program?
After running your first PHP program, explore PHP variables, form handling, and PHP database connection.
