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’.

Example:

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:

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.

<html>

<head>

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

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

</head>

<body>

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

</body>

</html>

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.

LETS PRINT HELLO WORLD TEXT:

Syntax:

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

Remember:

“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

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.