PHP Tutorial 2: PHP Script Syntax and HTML File Compatibility

php basics

This tutorial will guide you the basic syntax PHP supports. This is in fact the basic and important criteria for any programming language.

PHP code is executed at the server and a plain text result is sent to your browser. To execute this script effectively there has been a PHP script block. The complete PHP code has to be written inside this block.

//Code or PHP script

Servers that support shorthand support enables you to write the script in <?     ?> tags.  For maximum compatibility, it is always recommended to keep your script between <?php    ?> tag.

The important thing to be remembered as far PHP is considered is, The PHP script is executed by the server. You don’t need to put efforts to execute the script.

  • PHP within HTML page

Another feature that makes PHP more interesting is its easy compatibility with HTML file. You don’t need to work hard to make PHP script execute in HTML file. Just place the PHP script between the <body>  tag to execute the script. Important point to be remembered is when you have PHP script in HTML file save the code with .php extension and not with .html.

Simple example to show execution of PHP script within HTML file:

<p> Hello world from html tags</p>
echo “hello world from PHP script”;

echo is a print function. That is used to print contents on the screen this works same as printf and cout in C/C++. The out put of the above written code is.

php basics

  • Comments in PHP

Comments can also be added within a PHP script just like any other programming languages.
Single line comments can be added using  // [double slashes] multiple line comments can be added as

/*  comments */


// comments
echo “hello world”;
/* this is
multiple line comment */

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.