• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » PHP » PHP First program: the traditional “Hello World display …
Next →
← Prev

PHP First program: the traditional “Hello World display …

By Dinesh Thakur

In previous tutorials on working environments, we have shown that PHP is a dynamic scripting language interpreted and precompiled server side. It is now for us to achieve our first programs and run them on the web server (local or remote).

In computer programming, there is a “tradition” which is to generate the string (note the term the way) Hello World! standard output (in our case it is a computer screen). Let’s start with the first script shown below.

We’ll be covering the following topics in this tutorial:

  • First PHP script
  • Improved Hello World
  • First PHP program!

First PHP script

Enter the following code in a text editor (Note pad, Wordpad or Notepad ++ are largely the case) then save the file with the name helloworld.php.

Note: all files containing PHP code are required to be registered with the .php extension (or .phpX where X is the PHP version number).

First PHP program: “Hello World”

<?php echo 'Hello World!'; ?> 

Run this first script in a web browser (Safari, Firefox, Opera, Internet Explorer …). You see that the text Hello World! appears well on screen. So we get the desired result out. Time to explanations.

Code Explanation

First of the beacons. Any PHP script must be surrounded by two tags to delimit the other type of content contained in a single file (eg HTML). Here we use the <? Php and?>. There are others but they are strongly advised not to use. If you want to know what are they and why we should not use them, please see the following tutorial: Why it is not advisable to use short tags (short-tags)?.

Regardless, you should always use the markers of this first program. This is THE first good practice to adopt when you code in PHP.

The second part of the code is what is called a programming instruction. The echo () function (or rather the language structure because it is a particular function of PHP) is responsible for writing what is happening as a parameter to standard output. Here the setting is a string (type) whose value is “Hello World! “.

Important concept to remember: the PHP script is executed on the server and the result of this execution being returned (here the html code) is interpreted by the Web browser.

Improved Hello World

So far nothing difficult. Let us move to the next level. We will generate our Hello World! in the middle of an HTML document. Here the file code helloworld1.php:

 

Generating a minimal HTML document

<! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.1 // EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "en">
      <head>
           <title> First PHP program! </title>
      </head>
             <body>
                  <?php
                        echo 'Hello World!';
                     ?>
             </body>
</html>

First PHP program!

After running this file, we see that the result on screen is exactly the same as before. Yes, but only to the naked eye! Here we generated our string inside HTML. The principle of dynamic page begins to be felt from there. Indeed, PHP will allow us to generate pages from templates and parameters provided to it.

Suppose we want to display our Hello World! in bold. Two choices are available to us:

We place the <strong> and </strong> on either side of the script.
We place the <strong> and </strong> directly in the echo () statement.

Whatever the result is the same product. But the second example (see code below) shows while it is possible to generate HTML code to build a page. The advantage of PHP becomes evident and we can imagine all the possibilities available to us later. For example: generate HTML tables, lists, links, paragraphs, XML documents …

 

A “Hello World” in bold

<! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.1 // EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "en">
          <head>
                 <title> First PHP program! </title>
         </head>
                <body>
                     <?php
                             echo '<strong> Hello World </strong>';
                        ?>
                </body>
</html>

 

The code below will effectively write on the screen: Hello World!

You’ll also like:

  1. Python First Hello World Program
  2. Plasma displays, Thin film electro-luminescent display, Light-emitting diode ( LED ), Liquid Crystal Display (LCD)
  3. Write a C++ Program to Display Time.
  4. Write A C++ Program To Display Data Without Array.
  5. Write A C++ Program To Display Fibonacci Using Recursion.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

PHP Tutorials

PHP Tutorials

  • PHP - Home
  • PHP - Features
  • PHP - Magic Methods
  • PHP - Imagefilter
  • PHP - Arrays Numeric
  • PHP - Sessions
  • PHP - Forms Processing
  • PHP - clone()
  • PHP - Cookies
  • PHP - Variable Types
  • PHP - First program
  • PHP - call()
  • PHP - Iterator interface
  • PHP - Imports files
  • PHP - Exception Handling
  • PHP - set() and get()
  • PHP - Install MAMP
  • PHP - Functions
  • PHP - Constants Types
  • PHP - Comments Types
  • PHP - OOP's
  • PHP - OOps Use
  • PHP - PHP Code & Redirect 301
  • PHP - Control structures
  • PHP - Abstract Classes
  • PHP - Control structures
  • PHP - Classes
  • PHP - MySQL NULL values
  • PHP - Methods Visibility
  • PHP - Operator Types
  • PHP - Short tags Not use
  • PHP - Object and class
  • PHP - Secure Passwords

Other Links

  • PHP - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW