🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

PHP – First Program

Write your very first PHP program! Learn to output text with echo, understand the PHP opening tag, and run PHP live in your browser.

Your First PHP Program

It’s time to write your very first line of PHP! In programming, it is an age-old tradition to make a computer display the phrase “Hello World!” as your first exercise.

In HTML, you would simply type the text onto the screen. In PHP, we have to formally instruct the engine to “output” or “print” the text to the webpage before the HTML is built.

The echo Command

To output text or variables onto a screen in PHP, we predominantly use the universally popular echo statement.

Let’s look at the basic structure:

<?php
  // This is a PHP script
  echo "Hello World!";
?>

Breaking It Down

  1. <?php: This explicitly tells the web server that everything following it is PHP code and must be strictly processed by the engine, rather than output directly to the screen.
  2. echo: A built-in command instructing the engine to print strings, HTML fragments, or variables straight to the webpage body.
  3. "Hello World!": The actual text we want printed. In PHP, pure text (strings) must be surrounded by quotation marks.
  4. ;: The semicolon dictates that the statement has completely finished.
  5. ?>: This signals the end of the PHP processing block, instructing the server to return to processing normal HTML.

Combining PHP with HTML Elements

The true power of PHP comes from merging database logic and variables inside standard HTML architecture. You can actually echo out HTML tags directly, and the browser will render them perfectly!

<!DOCTYPE html>
<html>
<body>

  <h1>My Profile Dashboard</h1>
  
  <?php
    echo "<p>Welcome to my very first server-side script.</p>";
    echo "<b>Current Server Status: Online</b>";
  ?>
  
</body>
</html>

The person viewing your website will never see the <?php ... ?> tags. When they click “View Source” in their browser, they will only see the final, processed HTML output!

Try It Yourself!

The best way to learn PHP is to experiment. Try writing your own name dynamically using PHP below. See what happens if you accidentally delete the ending semicolon!

Preview