PHP Print a text – Echo Function

PHP is a server side scripting language and it helps to establish communication between client system and server. The best way to learn php is to begin understanding of basics of PHP. In this PHP Basics chapter series, lets begin with printing a message – ‘Hello world’.

PHP Echo Function:

Lets begin our process with simple job, printing a message.

For this, we need to use predefined function of PHP ‘ECHO’.


Lets see how to print a message. For this we need to create project and a php file. For our learning purpose we are going to create and use Index file. Follow following steps:

  • Start XAMPP (If you are not aware of how to start xampp, then read this article: Launch XAMPP )
  • Create project folder and name it. (I am going to name it, as ‘sample’)(If you are not aware how to create project in xampp, then read: Create Project in XAMPP)
  • Create index file. Use any simple text editor, open a new file and save it as “index.php”.

NOTE: index file is the default file, when a browser is about to load a website, it looks for the file.

NOTE: We are using .php extension because we are going to use a php file. (Remember, in PHP file we can also embed HTML tags and it would behave same to a HTML Page)

For coding simplicity, you can use sublime or notepad++. (This software will help us in easy coding. If you have not installed sublime or notepad++ then download and install them. Or you can simply use Notepad also).

Now in the index.php, first create the HTML Webpage structure as below.



<title>This is a sample webpage</title>

<meta name=”content” description=”this is a sample webpage for practice purpose.”/>



This is a sample webpage. This webpage is for practice purpose. I have successfully developed the webpage.



Save the file.

Open a browser (chrome, or firefox or safari or internet explorer) and visit following link (Assuming xampp is still running):

If you are using mac: http://localhost:8080/sample

If you are using windows: http://localhost/sample

Then following result page will appear.

PHP echo function

Okay. so we have successfully created a php file which is embedded with HTML Web Structure. If you are now aware of HTML Webpage Structure, then read: HTML WEBPAGE STRUCTURE

Now remove, the text in between body tag of our index page. Save it.



<?php echo “TEXT YOU WANT TO PRINT”; ?>


  • PHP code starts with “<?php” and ends with “?>”
  • PHP code always lies in between this opening and closing tags.
  • If we are creating a PHP only page where HTML is not embedded, then we don’t need end tag (“?>”). In future chapter we are going to learn about php only pages.

“echo” is a function predefined by php and is used to print text in the webpage. 

Now in between body tag of our index page, type following code:

<?php echo “Hello World”; ?>

Save the index file. Run the webpage.

If you are using mac: http://localhost:8080/sample

If you are using windows: http://localhost/sample

You would see following result.

PHP Echo function

  • In the code, note, there is a semicolon towards the end of the statement. In PHP, every statement ends with a semicolon. Here ‘echo “Hello World”‘ is a statement.
  • Why Hello World is kept inside double quote? Because Hello world is a string. In scripting language, when ever we provide string value, we put it inside double quote
  • In PHP, We can also use single quote to print a string value.
  • If you are familiar with programming language, such as C, C++, JAVA, C#, then we pass string value as parameter to print function. (That is, we keep string value inside the bracket of print function). In PHP, we can also pass string value as parameter.

Try the following code:

<?php echo(“Hello World”); ?>

The above code is not going to give any error. It will execute smoothly.

Practice, printing various texts as much as you can.

NOTE: We are going to use this same index file, to practice and learn other PHP Basics.