Back to course

Your First PHP Script: 'Hello World'

PHP: The Complete 0 to Hero Bootcamp

3. Your First PHP Script: 'Hello World'

Let's write the canonical first program.

The PHP Delimiters (Tags)

PHP code must always be enclosed within special delimiters (tags) so the server knows when to start and stop interpreting PHP.

The most common and recommended tag is the standard tag:

php

Creating the File

  1. Open your preferred code editor (like VS Code).
  2. Create a file named hello.php inside your htdocs directory.
  3. Add the following code:

php

This is HTML

<?php
    // The echo statement outputs text to the browser
    echo "<h2>Hello World from PHP!</h2>";
?>

<p>This part is also HTML.</p>

Running the Script

  1. Ensure Apache is running.
  2. Open your browser and navigate to: http://localhost/hello.php.

Key Takeaways:

  • PHP code lives between <?php and ?>.
  • PHP statements end with a semicolon (;).
  • The echo statement is used to display output to the browser.