
Write Your First Program
Writing hello world in PHP is also a good way to test your web server to make sure everything is installed and working correctly. Hello world is typically the first program you write when learning a new programming language.
Basic Syntax of a PHP Program
PHP code is written as blocks between php tags.
<?php // this is a comment - your code goes between these tags ?>
A PHP files are saved with a .php extension. When you make a request for a .php file, a web server will interpret the code and send back the text generated – usually a customized web page.
Display Text with PHP
You output text with the echo command. (Print also works, but is generally slower than echo.)
<?php echo "Hello, World!"; ?>
You can include your PHP in between HTML as a code snippet, or you can use PHP to echo all the HTML code for a web page.
Display an HTML Page
In this example we print out an entire web page using the echo command.
<?php echo "<!DOCTYPE html> <html lang='en' dir='ltr'> <head> <meta charset='utf-8'> <title>Hello, World!</title> </head> <body> <h1>Hello, World!</h1> </body> </html>"; ?>
You can extend this page to include whatever text you like, however it gives you no advantage over writing a regular HTML page. In the next example we will add some variables to make the code more useful.
Customize HTML Page Using Variables
You output text with the echo command. (Print also works, but is generally slower than echo.)
<?php $title = "Hello, World!"; $body = "It's a glorious day."; echo "<!DOCTYPE html> <html lang='en' dir='ltr'> <head> <meta charset='utf-8'> <title>$title</title> </head> <body> <h1>$title</h1> <p>$body</p> </body> </html>"; ?>
With variables for the title and body of this page at the top, we have created a template that can be edited. Editing code directly is intimidating though, so let’s add a form to edit our page.
Update Your Webpage With a Form
You output text with the echo command. (Print also works, but is generally slower than echo.)
<?php // initialize our variables with default values $title = "Hello, World!"; $body = "It's a glorious day."; // if the form was submitted replace our defaults with form data if($_SERVER["REQUEST_METHOD"] == "POST") { $title = $_POST["title"]; } if($_SERVER["REQUEST_METHOD"] == "POST") { $body = $_POST["body"]; } // print out the webpage echo "<!DOCTYPE html> <html lang='en' dir='ltr'> <head> <meta charset='utf-8'> <title>$title</title> </head> <body> <h1>$title</h1> <p>$body</p> <div style='text-align:center;'> <form action='blog.php' method='post'> <input type='text' name='title' value='$title' style='width:650px;'><br> <textarea name='body' rows='8' cols='80'>$body</textarea> <br><input type='submit'> </form> </div> </body> </html>"; ?>
Save this file as “blog.php” – or change the form action to match the name of your file. When you view this page, there will be a form that lets you edit your title and body text.
This tutorial is not meant to cover HTML. If the HTML code used in this example is confusing you may want to look at learning HTML, especially using the <form></form> tags. We will look at ways to improve this page in later tutorials looking at error handling, arrays, and database access.