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 HTML result to the browser.
Read Also: How to Install PHP on Windows 10
Basic PHP Syntax
This article is all about the main idea of fundamental PHP Syntax. The PHP Script can be used anywhere in the document within PHP tags along with HTML. There are various ways to draft the PHP code.
Standard PHP Syntax
The Standard Scripts start with <?php and ends with ?>. All 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
<?= echo "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.
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 left the rest of the things remain as it is.
In the end, the webserver sends the final output back to our web browser, which is entirely in HTML.
PHP Case Sensitivity
In PHP, Keyword, Functions, User Defined Functions, Classes are not Case Sensitive. However, all Variables are Case Sensitive.
Example
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 We are learning PHP We are learning PHP
Example
<?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 HTML result to the browser. As you learn PHP basics, you have to pay close attention to the syntax. I hope you get a complete idea of Fundamental PHP Syntax.