PHP Hello World
Creating a "Hello, World!" script in PHP is simple and a great way to get started with PHP programming. Here’s how you can do it:
Basic "Hello, World!" Script
-
Create a PHP File:
- Open your text editor or IDE.
- Create a new file and save it with a
.php
extension, e.g.,hello.php
.
-
Write the PHP Code:
- Add the following code to your
hello.php
file:
- Add the following code to your
php<?php
echo "Hello, World!";
?>
- Run the PHP Script:
- You need a web server with PHP installed to run your PHP script. You can use Apache with PHP (such as XAMPP, WAMP, MAMP, or a LAMP stack) or a built-in server for testing.
- Place your
hello.php
file in the web server's root directory (e.g.,htdocs
for XAMPP). - Open a web browser and navigate to
http://localhost/hello.php
.
You should see the output:
Hello, World!
Using PHP Built-in Server
PHP comes with a built-in web server that you can use for development purposes. Here's how to run your script using the built-in server:
-
Navigate to the Directory:
- Open a terminal or command prompt.
- Navigate to the directory where your
hello.php
file is located.
-
Start the Built-in Server:
- Run the following command:
shphp -S localhost:8000
- Access the Script in a Browser:
- Open a web browser and navigate to
http://localhost:8000/hello.php
.
- Open a web browser and navigate to
You should see the same "Hello, World!" message displayed in your browser.
Full Example
Here is the complete process in one place:
- Create a file named
hello.php
. - Write the following code in
hello.php
:
php<?php
echo "Hello, World!";
?>
- Run the PHP built-in server:
shphp -S localhost:8000
- Open a web browser and go to
http://localhost:8000/hello.php
.
This will display:
Hello, World!
This simple example demonstrates how to create and run a basic PHP script. From here, you can explore more complex PHP functionality and start building dynamic web applications.