Syntax


Syntax - The rules that must be followed to write properly structured code.

PHP's syntax and semantics are similar to most other programming languages with the addition that all PHP code is contained with specific tags, of sorts. All PHP code must be contained within the following tags.

<?php

?>

 

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

<?

?>

You can enable shorthand tags from php.ini file.

Example

<?php

 echo ‘Hello’;
?>

Or

<?

 echo ‘Hello’;
?>

Here echo is a predefine PHP statement which will use to directly print the output to the browser.

When you run these codes both will print the “Hello” word as an output to the browser.

In PHP the user defined functions, classes, core language keywords (for example if, else, while, echo etc.) are case-insensitive. Therefore the three echo statements in the following example are equal.

 

<?php

echo("Hello
");  

ECHO("Hello
"); 

EcHo("Hello
"); 

?>

All these will give the same output.

On the other hand all variables are case-sensitive.

Consider the following example. Only the first statement display the value as $name because $name, $Name, $NAME are three different variables.

 

<?php

echo("Hello $name
");  

echo ("Hello $ Name
"); 

echo ("Hello $ Name
"); 

?>

Whitespaces:

Whitespace is not visible on the screen, including spaces, tabs, and end-of-line characters i.e. carriage returns. In PHP whitespace doesn't matter in coding. You can break a single line statement to any number of lines or number of separate statements together on a single line.

You can write php code anywhere in the page within the php tags.

PHP and HTML Code:

 

My First PHP Page

 

<?php

echo "Hello! ";

echo "Hello! ";

echo "Hello! ";

echo "Hello! ";

?>

 

This will output the following:

Hello World! Hello World! Hello World! Hello World! Hello World!

The Semicolon:

It is necessary that every statement in PHP script shout be end with a semicolon (;). The semicolon signifies the end of a PHP statement and should never be forgotten. For example, if we repeated our "Hello!" code several times, then we would need to place a semicolon at the end of each statement.

<?php

echo "Hello! ";

echo "Hello! ";

echo "Hello! ";

echo "Hello! ";

?>

If there is missing semicolon it will through the error.

Saving a php page:

You can save php script page with .php extension to your XAMPP’s htdocs folder or if using WAMP you have to save your page in www folder.

Example:

Write the following code in any editor simply in notepad.

 

My First PHP Page

 

<?php

echo "Hello! ";

echo "Hello! ";

echo "Hello! ";

echo "Hello! ";

?>

 

Save this page with test.php in your WAMP’s www folder. Path should look like

<?php

C:\wamp\www\test.php.

After that open your browser and type the following url : localhost/test.php. You can get the following output.

Hello World! Hello World! Hello World! Hello World! Hello World!

 

If the output is now shown, check that your WAMP or XAMPP server is running or not. If it not, first run the server, then try the same url.

 

Comments in PHP

Comments in script are used, so than anyone can easily understand the logic behind the code. It is the piece of code that will not read/executed as part of program. You can put the comment anywhere in the PHP script like any other languages.

PHP supports several ways of commenting:

Single line comment: (//) or (#).

Simply put // before any line for comment.

Multiple line comment:

/*…..*/ This is the multiple line comment, you have to put (/*) before the line and (*/) after the line. This will make comment all the line inside this.

Example:

<?php

// This is a single-line comment

# This is also a single-line comment

/*

This is a multiple-lines comment block

that spans over multiple

lines

*/

// You can also use comments to leave out parts of a code line

$x = 5 /* + 15 */ + 5;

echo $x;

?>