Syntax and Comments
The syntax is the set of rules that defines the representation of programming language. It basically provides the way to write the code i.e. code format.
PHP script starts with the <?php and ends with ?>. The syntax of PHP is
<?php //code statement echo "Hello World"; ?>
Every statement of PHP script must end with the ;. This tells the compiler that the statement ends from here.
Short Open Tag
It is not compulsory that we have to start the PHP script with <?php. We have also another code i.e short-open-tag that we can use instead of <?php.You can use short open tag as given below:
<? echo "Hello World"; ?>
This method is working fine in the XAMPP server but may not work in the WAMP server. If the short open tag doesn’t work on your server then you have to enable the short_open_tag from the php.ini file.
PHP Script inside HTML
PHP files are the plain text with .php extensions. We can write HTML inside the PHP file and also we can write PHP script and HTML code as a mix.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Basic PHP File</title> </head> <body> <h1><?php echo "Hello, world!"; ?></h1> </body> </html>
The above example shows you how to embed the PHP code inside the HTML code. As with the above code, We can write our PHP code anywhere using <?php ….. ?>.
In the above example, the PHP compiler compiles the code inside the <?php ….. ?> and displays the “Hello World” as output then after the browser will render HTML code.
Comments are the explanation or description of source code that does not affect your program logic in any way. They are neglected by compilers or interpreters.
PHP supports both single-line comments and multiline comments. We can write comments either start with # or with // or /* ——– */.
// This is the single line comment # This is also single line comment /* * This is multi line comment */
All the comments are ignored by the compiler while compiling the code. Let’s see an example to make clear concepts on comments.
<?php // This is the single line comment # This is also single line comment /* * This is multi line comment */ echo "Hello World"; ?>
The output of the above program is Hello World.