PHP - Hello World
Introductionβ
The "Hello World" program is a common starting point for beginners learning a new programming language. Let's dive into creating a simple "Hello World" program in PHP using XAMPP.
Prerequisitesβ
Ensure that you have XAMPP installed, which includes the Apache server and PHP module.
Writing and Running the Programβ
- Open your preferred text editor.
- Save the following code as
hello.php
in thehtdocs
directory of your XAMPP installation.
<?php
echo "Hello World!";
?>
- Start the Apache server from the XAMPP control panel if it's not already running.
- Open a new tab in your browser and enter
http://localhost/hello.php
as the URL. - You should see the "Hello World" message displayed in the browser window.
Mixing HTML with PHPβ
You can mix HTML and PHP code in a single script. For example:
<!DOCTYPE html>
<html>
<body>
<h1>My PHP Website</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
The "Hello World" message will be rendered as plain text, but you can include HTML tags inside the PHP code for formatting.
Running PHP Scripts from Command Promptβ
You can also run PHP scripts from the command prompt. Assuming you have a hello.php
file with the following content:
<?php
echo "Hello PHP!!!!!";
?>
- Add the path of the PHP executable (e.g.,
php.exe
in XAMPP) to your operating systemβs path environment variable. - Open a command prompt and navigate to the directory containing
hello.php
. - Run the script using the command:
php hello.php
You will see the output:
Hello PHP!!!!!
note
Ensure that XAMPP is properly configured and the Apache server is running to execute PHP scripts.