PHP’s syntax and semantics are most similar to the other programming languages like C,Perl, Python with the addition that all PHP code is contained with a tag, of sorts. All PHP code must be contained within the following Syntax..

<?php
?>

or the shorthand PHP tag that requires shorthand support to be enabled
on your server...

<?
?>

If you are writing PHP script and plan on upload in live server, we suggest that you can the standard format (which includes the <?php ?>) rather than the shorthand form. This will ensure that your scripts will work, even when running on other hosting servers with different settings.

Save PHP File:

For suppose you have PHP included into your HTML and want to execute the file in the web browser to interpret it correctly, then you must save the file with a .php extension, instead of the standard .html extension. Instead of index.html, it should be index.php if there is PHP code in the file.

PHP code can be written right into your HTML, like this:

<!DOCTYPE html>
<html>
	<head>
	</head>
	<body>
        <p>
          <?php
        echo "This is my first php program";
          ?>
        </p>   
	</body>
</html>

Your PHP code inside of the delimiters. Here we use the function echo to output “This is my first php program””. We also end the line with a semicolon.

If you save this file (e.g. myfrist.php) and place it on PHP enabled server and load it up in your web browser, then you should see “This is my first php program” displayed.

Result:

As you observed the above example, there was a semicolon after the line of PHP code. The semicolon signifies the end of a PHP statement and should be mandatory.

0 Shares