PHP Syntax

Last updated on

The PHP tag, or syntax, is a set of rules and guide lines for PHP code. Therefore, when writing code in PHP, you have to follow it; otherwise, the interpreter will ignore the code execution.

Before getting started, it is essential to know that the file name of a PHP document ends with “.PHP” as an extension.

Actually, there are many types of PHP tags, which are:

  • Opening and Closing PHP Syntax.
  • PHP Short Echo.
  • PHP Short Tags.
  • And some other tags were already working before PHP 7.0.

Anyway, let’s move onto the following section to understand how the default tag work in PHP.

Opening and Closing PHP Syntax

Opening and closing tags in PHP are similar to “hello” and “goodbye” in a conversation. So on computer, you have to start the document with “<?php” and close it with “?>”.Between these two syntaxes, the PHP blocks.

For example:-   

<?php 
   echo "Hello CodedTag";
?>

You can also open the PHP code with the “<?php” syntax and keep it open without a closing tag. However, you need to use a semicolon “;” at the end of each line.

Here is an example:

<?php 

  $val = “CodedTag Tutorial”;
  echo $val;

Additionally, you can write the last line within a PHP script without a semicolon, but in this case, you must use the closing tag. Otherwise, it will display a syntax error.

For example:

<?php 
   echo “This is a PHP tutorial \n”;
   echo “This line is wrapped with a semicolon \n”;
   echo “This line has no semicolon”
?>

Anyway, PHP also supports shorthand echo. Let’s move into the following section to understand how it works.

PHP Shorthand Echo

The shorthand echo is a quicker way to print or display data in PHP. So, when you want to show something on the webpage, you have to write the word `echo` followed by the data or variable. But the shorthand echo can perform this task more swiftly.

Observe the standard echo in PHP, which can be written as shown in the code below.

<?php echo "Standard PHP echo" ?>

This can be written in shorthand, as shown below:

<?= "Shorthand echo" ?>

So, you can use this syntax when mixing PHP code with HTML. It also makes the code cleaner and easier to read.

However, remember that this shorthand only works if your PHP settings (specifically short_open_tag ) allow it.

Anyway, let’s move on to another pattern for short tags in PHP, which also depends on the PHP setting short_open_tag.

PHP Short Tags

PHP short tag is a shorter version of PHP standard opening tags.

Here is an example:

<? /* Code Here */ ?>

We can use the short tags in PHP document to reduce the amount of typing and make the code look neater. Especially when using the embedded PHP code within HTML.

But remember to enable short_open_tag in your PHP configuration file (php.ini). If it’s set to “On” short tags can be used.

However, if you are using XAMPP Server on your Windows operating system, you will find the path of ‘php.ini’ in the following location:.

C:\xampp\php\php.ini

Anyway, let’s explore other PHP syntaxes in the following section.

Some Other Tags in PHP

As you may know, PHP has a long history, and with each new version, some older syntaxes and features are removed. They are doing that to improve the language performance.

So, in PHP 7.0 and later versions, several old syntaxes no longer work, such as HTML Script Tags and ASP Tags.

Here are examples if you prefer to work with a version prior to 7.0.

HTML Script Tags:

<script language="php">
  echo "This is a script syntax";
</script>

ASP Tags:

<%  echo "This is a ASP syntax"; %>

Anyway, let’s summarize it in a few points.

Wrapping Up

In this tutorial, you learned the basics of PHP syntax, which are important for writing a PHP document.

Here is a simple summary:

  • A PHP file name should end with the “.php” extension.
  • PHP has standard tags, which are the opening and closing tags:<?php /* your code here */ ?>
  • The short tag is a briefer version of the standard PHP tags, written as <? "text" ?>. It also includes a short print tag, written as <?= "Your string!" ?>.

Thank you for reading. Happy Coding!

Share on: