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
- Open your preferred code editor (like VS Code).
- Create a file named
hello.phpinside yourhtdocsdirectory. - 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
- Ensure Apache is running.
- Open your browser and navigate to:
http://localhost/hello.php.
Key Takeaways:
- PHP code lives between
<?phpand?>. - PHP statements end with a semicolon (
;). - The
echostatement is used to display output to the browser.