PHP Hello World

Summary: in this tutorial, you’ll learn how to execute a script that outputs the Hello, World! message on the web browser and command line.

PHP Hello World on the web browser #

First, open the folder htdocs under the xampp folder. Typically, it is located at C:\xampp\htdocs.

Second, create a new folder called helloworld.

Third, create a new file called index.php under the helloworld folder and place the following code in the file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP - Hello, World!</title>
</head>
<body>
        <h1><?php echo 'Hello, World!'; ?></h1>
</body>
</html>Code language: PHP (php)

Try it

The code in the index.php file looks like a regular HTML document except for the part <?php and ?>.

The code between the opening tag <?php and closing tag ?> is PHP:

<?php echo 'Hello, World!'; ?>Code language: PHP (php)

Try it

This PHP code prints out the Hello, World message inside the h1 tag using the echo statement:

When PHP executes the index.php file, it evaluates the code and returns the Hello, World! message.

Fourth, launch a web browser and open the URL:

http://localhost:8080/helloworld/Code language: PHP (php)

If you see the following on the web browser, then you’ve successfully executed the first PHP script: