PHP Tutorials - PHP Syntax - LoudProgrammer

PHP Tutorials – PHP Syntax

free-php-mysql-tutorials-course-online-tutorial

PHP scripts can be placed anywhere inside an HTML document. You can place them at the beginning of your HTML, inside the HTML tags or at the end of the HTML document.

In this tutorial we are going to write a simple script that is going to print an “Hello World!” string to the browser to demonstrate the syntax of PHP code. You can use any text editor of your choice to write and edit PHP code.

Create a new text document file and save it in your local computer htdocs folder as helloword.php . All PHP scripts must have a file extension of .php in order to executed by the server.

After creating the file open it in your text editor of choice and type in the following code:

We are going to include our PHP code in between the HTML <body> tags. PHP scripts begin with an opening tag <?php  and ends with the closing tag ?> . In between the tags you can place your php code and have them executed by the server.

We are going to use the PHP  echo ""  statement to print an ‘Hello World’  H1 heading string to the browser. You place the text to be printed to the browser in between the double or single quotes in the echo statement.

As you have noticed above, all PHP statements have to be terminated with a semicolon to mark the end of the statement. Save your file, start your web server and load the file by pointing your web browser to the url

http://localhost/helloworld.php

Remember to include the file name together with the file extension in the url: in our case .php so that that server recognizes that it’s a PHP script to be executed.

If everything was done right you should the string below printed out in your browser window.

Hello World!

That is the basic PHP syntax that you would use throughout your PHP applications.

Found this article useful? Please share your thoughts below.

You might also like the article on PHP Tutorials – How to Include Comments in PHP

The following two tabs change content below.
Geoffrey is a lead software developer, author and writer. He writes code from scratch but frequents GitHub. He also writes and talks about technology trends, small business tips and developer productivity hacks. He is no coffee addict.

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not a robot. *