×
☰ See All Chapters

PHP Syntax

A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. Below, we have an example of a simple PHP script which sends the text "Hello World!" to the browser:

<!DOCTYPE HTML>

<html>

<head>

<title>PHP Example</title>

</head>

<body>

<?php

echo 'Hello World!';

?>

</body>

</html>

Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another. The last line of the code can be written without semicolon.

<?

echo 'Hello World!';

echo 'Hi'

?>

PHP code must be enclosed inside predefined style tags. Below table give you the list of declaration style tags of PHP statements.

Declaration Style Tags of PHP statements

Tag Style

Start Tag

End Tag

Description

Standard tags

<?php

?>

This style supports all functionalities.

Example:

<?php

echo 'Hello World!';

?>

Short tags

<?

?>

This style supports few functionality. By default this style is disabled, by changing the settings in php.ini file this can be enabled.

Example:

<?

echo 'Hello World!';

?>

ASP tags

<%

%>

With this style you can ass PHP code inside ASP declaration. By default this style is disabled, by changing the settings in php.ini file this can be enabled.

Example:

<%

echo 'Hello World!';

%>

Script tags

<script language="php" >

</script >

This is similar to javascript declaration syntax and supports all functionalities as standard style tag.

Example:

<script language="php" >

echo 'Hello World!';

</script>  

Comments in PHP

In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.

<html>

<body>

<?php

//This is a comment

/*

This is

a comment

block

*/

?>

</body>

</html>

PHP Include

You can create any number of PHP files. It is vital to organize you code create multiple file in real time project. To reuse code you have to include code from one file to another file, otherwise you have to duplicate code. To include PHP file inside another PHP file you have to use include keyword with the below syntax.

include "path to php file";

Path can be absolute or relative path. You can write include statement anywhere inside the file, but should be before using the included file’s code.

PHP Include Example

php-syntax-0

With the above project files, when you include all files in index.php it looks like below:

<?php

include "F:/My_Programs/PHP/Example/files/employee.php";

include "elements/student.php";

include "../demo.php";

include "hello.php";

?>

 


All Chapters
Author