PHP Syntax

A PHP file contains HTML tags with some PHP code. It is straightforward to create the PHP file. First, create a file and write PHP + HTML Code. A PHP script executes on the server and returns the HTML result to the browser.

Read Also: How to Install PHP on Windows 10

Basic PHP Syntax

The main aim of this article is to clarify all concepts related to PHP Syntax. The PHP Script can be used anywhere in an HTML document by enclosing it within the PHP tags. There are various ways to draft the PHP code.

Standard PHP Syntax

The Standard Scripts start with <?php and ends with ?>. PHP code is always inside the PHP code tags. These tags are called “Canonical PHP Tags.” Every PHP Command ends with a Semi-Colon (;).

Example

<?php 

echo "Hello, world!";

?> 

Output

Hello, world!

Short Open Tag

A short open tag is the shortest option to initialize a PHP Code. The PHP scripts start with <?= and ends with ?>. A short open tag is only available when the short_open_tag option is On in the php.ini configuration file.

Example

<?=

"Hello, world!";

?> 

Output

Hello, world! 

HTML Script Tag

These Script tags can be used just like we do for adding javascript code in the HTML document. It is outdated and not recommended.

Example

<script language="php"> 

echo "hello world!"; 

</script> 

Output

Hello, world!

Asp Style Tags

These tags look like <% … %> and <%= … %>. These are used by active server pages to describe the coding blocks. They are no longer available from PHP 7.

 Example

<% 

echo "hello world"; 

%> 

Output

Hello, world!

Embedding PHP Within HTML

A PHP file contains HTML tags and some PHP code. It is straightforward to create the PHP file. First, create a file and write PHP + HTML code and save it with extension .php.

Example

<html>

<head>

<title>A Simple PHP Code</title>

</head>

<body>

<h1><?php echo "Hello World !" ?></h1>

</body>

</html>

Output

Hello World!

Explanation

The example above shows how we can embed PHP codes within HTML tags. When we run this code, the PHP engine processes the instructions between the PHP tags (“<?php echo “Hello World !” ?>”) and does not execute or affect the rest of the code.

In the end, the webserver sends the final output back to our web browser, which is displayed with the help of HTML.

PHP Case Sensitivity

In PHP, Keyword, Functions, User Defined Functions, Classes are not Case Sensitive. However, all Variables are Case Sensitive.

Example 1

In this example, we can see all the echo statements are equal and valid.

<?php

echo("We are learning PHP on errorsea<br>"); 

ECHO("We are learning PHP on errorsea<br>");

EcHo("We are learning PHP on errorsea<br>");

?>

Output

We are learning PHP on errorsea
We are learning PHP on errorsea
We are learning PHP on errorsea

Example 2

<?php

$amount = 200;

echo("The Amount is : $amount <br>"); 

echo("The Amount is : $AMOUNT <br>");

echo("The Amount is : $amoUNT <br>");

?>

Output

The Amount is : 200 
The Amount is :
The Amount is :

In this example, only the first statement displays the value as $amount because all three variables are different.

Read More: PHP Comments

Conclusion

A PHP Script executes on the server and returns the result to the front-end which is then displayed with the help of HTML. As you learn PHP basics, you have to pay close attention to the syntax.

We hope you have been able to get a complete idea of the fundamentals of PHP Syntax. Stay determined on your mission to conquer the PHP Language and Happy Learning!

Leave a Reply

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