PHP syntax, tags, echo, comments, hello world

PHP Syntax: The Ultimate Guide

In this tutorial, we’ll discuss the basic PHP syntax in detail. We’ll explore popular PHP editors. You’ll learn PHP tags, echo statements, the use of semicolons to terminate instructions, case sensitivity, PHP comments, and embedding PHP in HTML documents. After understanding basic PHP syntax, we’ll create our first PHP “Hello World” page and learn about PHP file extensions.

Before you learn about the PHP syntax and write PHP code, you’ll need to install & configure a PHP development environment on your PC.


Code Editors/IDEs to write PHP code

So, you are ready to understand PHP syntax and start writing great web applications with PHP. Wait a minute! Where exactly do you write that PHP code?

Although, you can use a very basic text editor like “Windows Notepad” to write PHP code. However, it is never recommended because of inefficiency and lack of productivity.

On the other hand, a high-end code editor/IDE takes minimum time to write good-quality PHP code. It highlights the sections of PHP syntax and gives auto-fill & auto-complete suggestions. It also checks PHP syntax for errors, validates PHP syntax, and intelligently performs debugging of PHP syntax. Thus, it tremendously improves the speed, efficiency, and productivity of a PHP web developer to create large web applications.

Difference between code editors and IDEs

Although the terms “code editor” and “IDE” are used interchangeably, both have differences.

Code editors are single-purpose, support multiple languages, and just help in writing code faster. IDEs are multipurpose and usually support a single language for which they are built. IDEs help in writing, compiling, executing, and debugging code.

Choosing between the two is a matter of personal preference. If you have just started writing PHP code, a good code editor will do more than enough for you. However, if you are an expert PHP developer and work on large-scale projects, IDEs are a better choice for you.

Popular code editors for writing PHP code

Some of the most popular code editors (both free and paid) for writing PHP code include:

Popular IDEs for writing PHP code

Some of the most popular IDEs (integrated development environment) for PHP include:

  • PHPStorm
  • Zend Studio
  • Visual Studio
  • Komodo IDE
  • NetBeans
  • Eclipse

PHP Syntax: Basic PHP tags

If you ever work with HTML, you’ll know that all HTML documents consist of HTML tags. Similarly, we use opening and closing tags to write PHP code.

Opening PHP tags

<?php

Closing PHP tags

?>

We know that PHP is embedded in all sorts of documents especially HTML documents to create web applications. So, the PHP parser looks for the opening and closing PHP tags when reading a PHP file while ignoring everything else.

<?php

// All PHP code goes here between opening and closing tags.

?>

PHP short open tag

You can also write PHP code by using the short open tag <?. Remember that short tags only work when the short_open_tag directive is enabled inside the php.ini file configuration. Although short tags are available by default, they can be disabled in the PHP/web server’s configuration. It is, therefore, recommended to use normal opening and closing tags.

<?

// All PHP code goes here between opening and closing tags.

?>

Short echo tag in PHP

The echo statement is used to return output in PHP. We usually use it in the following way:

<?php

echo "My First Echo Statement";

?>

The above block of code will output something like this in your browser.

My First Echo Statement

However, PHP allows us to use echo statement in a shorter format by removing the word echo. This will only be useful if you want to get output from a single-line code.

<?= "My First Echo Statement"; ?>

Above short echo statement will produce the same output as shown below.

My First Echo Statement

Omit the closing PHP tag

When you are creating a file with only PHP code, it is usually a good practice to omit the closing ?> PHP tag. This helps in preventing fatal PHP errors which may arise because of accidentally adding whitespace or lines of code after the closing tag. However, if your PHP code is embedded in HTML, you should always use both opening and closing tags.

<?php

echo "Omit the PHP closing tag in PHP only files";

// another line of code

echo "Avoid fatal errors";

// the PHP code without a closing tag ends

PHP Syntax: Echo statement in PHP

In PHP, we use echo statements to get output. An echo statement is used either to display a piece of text or to display output from earlier defined variables/functions.

<?php

	echo "This echo statement displays text.<br>";

	$stock_in = 35;
	$stock_out = 23;
	$stock_balance = $stock_in-$stock_out;
    
    echo "Remaining stock inventory is $stock_balance.";
	
?>

As you can see that above lines of code display the following output in the web browser.

This echo statement displays text.
Remaining stock inventory is 12.

Additionally, we can use an echo statement with multiple parameters/arguments as shown below.

<?php

	$stock_in = 35;
	$stock_out = 23;
	$stock_balance = $stock_in-$stock_out;

    echo "Remaining stock inventory is " . $stock_balance . " items in store.";
	
?>

The code above displays the following output in the web browser.

Remaining stock inventory is 12 items in store.

Embedding HTML in echo statements

We can also embed different HTML tags inside echo statements to manipulate the output of echo statements. For example,

<?php

	$stock_in = 35;
	$stock_out = 23;
	$stock_balance = $stock_in-$stock_out;
    
    echo "Remaining stock <strong>inventory</strong> is " . $stock_balance . ".<br>";
	echo "<h2>Congratulations! You are successful.</h2>";
	
?>

The code above has an HTML <strong> tag to bold the text, a <br> tag to insert a line break, and a <h2> tag to create a heading. It displays the following output in the web browser.

echo statement in PHP

PHP Syntax: Use of semicolons

Just like many other programming languages, PHP also requires that you should terminate all statements individually. We use semicolons (;) at the end of each statement in PHP to separate instructions.

<?php

$variable1 = 10;
$variable2 = 8;
$variable3 = $variable1+$variable2;

echo "Semicolon at the end of first statement";

echo "Semicolon at the end of second statement";

?>

However, you may omit the semicolon after the last line of the PHP code block just before the closing PHP tag. But, it is usually a best practice to use semicolons at the end of each statement to prevent fatal errors in PHP.

<?php

$variable1 = 10;
$variable2 = 8;
$variable3 = $variable1+$variable2;

echo "Semicolon at the end of all statements";

echo "No semicolon at the end of last statement"

?>

If you are embedding a single-line PHP statement inside HTML, you may omit the semicolon at the end of that statement. But if you are using multiple lines of PHP statements inside HTML, you should always use a semicolon at the end of each statement along with opening and closing PHP tags.

<!DOCTYPE html>
<head><title>Single Line PHP</title></head>
<body>

<?php echo "Omit semicolon in a single line PHP statment." ?>

</body>
</html>

PHP Syntax: Case sensitivity

Unlike many other programming languages, general PHP syntax including different statements (echo, print, if, else, while, do-while, etc.), classes, functions, expressions, and user-defined functions is not case-sensitive. For example, same statement given below with different cases results in similar output.

<!DOCTYPE html>
<html>
<head><title>Case Sensitivity in PHP</title></head>
<body>

<?php
$input = 99;
$output = 125;
$profit = $output-$input;

EcHo "Your profit is $profit <br>";
echo "Your profit is $profit <br>";
ECHO "Your profit is $profit <br>";
?>

</body>
</html>

The code above produces the same output in the browser.

Your profit is 26
Your profit is 26
Your profit is 26

However, you must remember that all variable names in PHP are highly case-sensitive and will work only if the correct case is called. We’ll just change the case of the variable $profit in the example above.

<!DOCTYPE html>
<html>
<head><title>Case Sensitivity in PHP</title></head>
<body>

<?php
$input = 99;
$output = 125;
$profit = $output-$input;

EcHo "Your profit is $Profit <br>";
echo "Your profit is $proFit <br>";
ECHO "Your profit is $profit <br>";
?>

</body>
</html>

The code above outputs the result shown below. You can see that the first two echo statements have given a warning because of case sensitivity. Only the last echo statement has given the desired result.

Warning: Undefined variable $Profit in D:\xampp\htdocs\hello.php on line 11
Your profit is

Warning: Undefined variable $proFit in D:\xampp\htdocs\hello.php on line 12
Your profit is
Your profit is 26

PHP Syntax: Using comments in PHP code

PHP comments are the lines of code that are never executed by the PHP parser. The comments help you to divide your PHP code into different sections based on its functionality. This helps to describe different parts of your code in a better way.

Why are comments important?

If there are no comments in the PHP code, a developer will feel lost when has to work even on his own previously coded application. For example, if you start working on your own program two or three years later, you will not understand the purpose of different sections of code and why & how you wrote them.

Similarly, if a web developer has to work with PHP code written by another developer, comments will help him better understand the code.

PHP supports Perl, C, or C++ like comments. You can write PHP comments in two different ways.

One-line PHP comments

You can use one-line comments either to describe a single line of code or simply to prevent the execution of a single line of code.

PHP allows you to write one-line comments either by using the popular hashtag symbol # or a double slash //.

To describe a single line of code, use # or // at the end of a statement. To prevent the execution of a single line of code, use # or // at the start of a statement.

<?php

    echo "code executed."; # This comment describes this line of code.

    # echo "This line of code will not be executed.";
	
    echo "code executed."; // This comment describes this line of code.

    // echo "This line of code will not be executed.";
	
?>

Multiple-line PHP comments

You can use multiple-line comments either to describe a large block of code or simply to prevent the execution of a large block of code.

Multiple-line PHP comments start with /* and end with */. Anything written in between these two symbols will not be executed by the PHP parser.

<?php

/* This comment describes the section 
	of code written below. This is great. */
	
    echo "code executed.";
    echo "This line of code will be executed.";
    echo "code executed.";
    echo "This line of code will be executed.";
	
/* This comment prevents the execution 
	of below written code. This is great. 
	
    echo "code not executed.";

    echo "This line of code will not be executed.";
	
    echo "code not executed.";

    echo "This line of code will not be executed.";
	
	*/
?>

How to embed PHP in HTML documents?

The power of PHP comes from its ability to embed into several types of documents, especially HTML documents.

To embed PHP inside an HTML document, you just have to write the opening <?php and closing ?> PHP tags inside HTML documents anywhere after the opening body <body> tag.

<!DOCTYPE html>
<html>
<head><title>Embedding PHP in HTML</title></head>
<body>

<!-- Embedding PHP Example 1 -->

<h1>Embed PHP in HTML Documents</h1>

<h3><?php echo "PHP is a powerful language." ?></h3>

<!-- Embedding PHP Example 2 -->

<h1>Embed PHP in HTML 2</h1>

<h3><?php
	
	$coderounds = "Coderounds.com is an excellent resource.";
	
	echo $coderounds;

?>
</body>
</html>

The code above gives the following output. You can see that anything in between the opening and closing PHP tags is interpreted by the PHP interpreter and everything outside is left as such.

embedding PHP inside HTML

Create your first “Hello World” page in PHP

In this article, you have learned about the PHP syntax in detail. Now is the right time to create your first PHP-enabled page before learning more PHP fundamentals and advanced functions.

As you know, a PHP file is just like a regular HTML file except that it contains the opening and closing PHP tags. To save a regular HTML file with PHP code, all you have to do is to save that file in our code editor with the “.php” extension instead of the “.html” extension.

Now, we will create our first PHP-enabled page using a text editor like notepad++, XAMPP server to interpret that PHP file (learn), and a web browser to display the output from the PHP file.

1 – Open your text editor and write some PHP code

first hello world page

2 – Save your file with the .php extension

first hello world page

3 – Open htdocs folder inside XAMPP and copy your file here

first php hello world page

4 – Start XAMPP control and click the “start” buttons in front of Apache and MySQL

first php hello world page

5 – Open your web browser and access your file by visiting http://localhost/helloworld/helloworld.php

first php hello world page

You have learned PHP syntax in detail. Your next step to learning PHP is to understand PHP variables.

Similar Posts

Leave a Reply

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