PHP Syntax

- LAST UPDATED

If you went through php introduction, you might already have got an idea about PHP and how PHP works. So PHP will return HTML codes to browsers after being executed in the server. We need to follow some specific standards and rules when writing PHP codes. The vital thing to render a file using PHP should be enclosed in PHP open and closed tags. There are exceptions for this, but in the usual way, we need to enclose the entire PHP code in the PHP open and close tags.

PHP Syntax Example

PHP open and close tags are <?php and ?>

<?php
PHP CODES HERE
?>

Usually developers use .php extension for PHP files. As example index.php, home.php etc. PHP files can have HTML codes which will render along with PHP outputs.

Here it is a complete example of a single PHP and its output. 

<!DOCTYPE html>
<html>
<head>
	<title>Sample PHP File</title>
</head>
<body>
<?php
	echo "Hello World";
?>
</body>
</html>

The above example shows that the PHP open-close tags are only used for the echo function. The echo function in PHP uses to display data from the PHP script. Having multiple PHP script tags on a single page is okay, and PHP will only execute the code inside the tags. The rest of the codes will be rendered as standard HTML code. 

All the PHP statements should end with a . The semicolon is unnecessary when terminating the last line of a PHP block.