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 (;).
<?php echo "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.
<?= "Hello, world!"; ?>
HTML Script Tag
<script language="php"> echo "hello world!"; </script>
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.
<% echo "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.
<html> <head> <title>A Simple PHP Code</title> </head> <body> <h1><?php echo "Hello World !" ?></h1> </body> </html>
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.
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>"); ?>
We are learning PHP on errorsea We are learning PHP on errorsea We are learning PHP on errorsea
<?php $amount = 200; echo("The Amount is : $amount <br>"); echo("The Amount is : $AMOUNT <br>"); echo("The Amount is : $amoUNT <br>"); ?>
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
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!