Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

PHP - Syntax Overview

There's a parsing engine in PHP which work is to differentiate PHP code inside a page from other possible scripting element such as HTML, JavaScript, etc. The parsing engine look for Canonical PHP tags to look for opening and closing snippet of a PHP code. This mechanism is called "Escaping to PHP"

Topics to be covered

We will cover the following topics in this lesson

  • Syntax Overview
  • How Commenting works in PHP
  • Outstanding Features of PHP
  • Hello World

PHP - Syntax Overview

For example

<?php
Some code here
?>

HERE,

  • <?php is beginning tag to inform the parsing engine that PHP code starts from here whereas the
  • ?> is end of code tag.

Anything that lies between these two tags will be considered PHP code. However within these tags HTML code can be inserted too and PHP engine will parse it smoothly, the way of doing so is:

Create a new file overview.php in your project directory and add the following code.

<?php

$thing = "Box";

echo "This is a " . $thing . "and its color is <font color=red> RED ";

?>

HERE,

  • $thing = "Box"; defines a string variable that is assigned the value of Box
  • echo "This is a " . $thing . "and its color is <font color=red> RED </font>"; prints a string that uses the variable value and some HTML mark.

Run the following code to start the built-in PHP server in the project directory

php -S localhost:8000

HERE,

  • The above command starts the built-in server on PORT number 8000

Load the following URL into your web browser


You should be able to see the following results

PHP Syntax Overview

This is a Box and its color is RED

As shown above, HTML code can be inserted inside the echo statement within the quotes but as soon as you break the code you are back to PHP area and as long as semi colon is not used it will be read as one statement only thus it will return the output as shown above.

How Commenting works in PHP

As with any other programming language and web scripting PHP also provides option for adding comments within the code.

A comment is basically any line or segment in the code that doesn't render on browser or when executed rather it is there for the code reader to understand instruction given by the coder.

It is mostly used for the coder himself for his own reference as well or his team so when he open the code after a while he knows what the line or segment below does. There are basically two types of commenting in PHP and as many as 3 styles can be used, for instance:

Single Line Comments

Single line comment example 1:

<?php

# This is my first code. Below syntax will output Hello World! in web browser

echo "Hello World!";

?>

HERE,

  • # This... the hash symbol before the string tells the PHP compiler that this is a comment.

Single line comment example 2:

<?php

// This is my first code in PHP. Below syntax will output Hello World! In web browser

echo "Hello World!";

?>

HERE,

  • // This the double forward slashes tell the PHP compiler that this is a comment. The double forward slashes is more commonly used compared to the hash symbol.

Multiple Lines Comments

Multiple lines commenting using standard method:

<?php

/* Hi, this is my first ever code
that I have written in PHP
and it is about orthodox Hello World code
*/

echo "Hello World!";

?>

HERE,

  • /* marks the beginning of the multiple comment. Anything that follows can span a number of lines and will be considered a comment.
  • */ marks the end of our multiple comment.

Using single line commenting characters as multiple lines commenting example 1:

<?php

// First line of comment
// Second line of comment

echo "it works!";

?>

Using single line commenting characters as multiple lines commenting example 2:

<?php

# First line of comment
# Second line of comment

echo "it works!";

?>

Using single line commenting characters as multiple lines commenting example 3:

<?php

// First line of comment
# Second line of comment

echo "it works!";

?>

 

Sensitivity and Insensitivity

Whitespace

Whitespace: PHP is mostly not sensitive to whitespaces, Whitespace means any character which is invisible in console but takes a space in code, such as a space, tab or carriage returns (such as EOL characters). So as long as a semi colon is not inserted at the end of statement PHP will parse it as a single line or same syntax even if you have passed return carriage.

For example:

$variable = 18 + 18;

Above is an example of single line syntax written with single spaces

$variable   =   18  +   18  ;

Above is an example of single line syntax written with multiple tabs

$variable =

18

+

18

;

Above is an example of single line syntax broken in to multiple lines with return carriage.

In all of the above examples PHP will parse the statement is a single syntax and will not process spaces or tabs neither it will consider carriage return as next or new syntax.

Case Sensitivity

Case Sensitive: PHP is a case sensitive language so any variable or anything case sensitive in the code segment will work according to the case sensitivity rules.

For example:

<?php

$room = 303;

echo "Your room # is $room<br>";

echo "Your room # is $RooM<br>";

?>

HERE,

  • $room = 303; defines a variable $room that is all lower case letters.
  • echo "Your room # is $room<br>"; prints the value of the variable $room
  • echo "Your room # is $RooM<br>"; prints the value of the variable $RooM. Notice we the second variable used upper case letter R and upper case letter M at the end.

The above code outputs the following

Your room # is 303

Your room # is

Because variable with capital R and M has not been defined.

The Terminator of Statement

As discussed above, PHP syntax EOL is determined by a semi colon at the end of the syntax instead of return carriage nor any other EOL character used in other languages.

For example:

<?php

$room = 88

$floor = "13th"

echo "Room # $room is on $floor"

?>

Above code will return error instead of giving out desired output

<?php

$room = 88;

$floor = "13th";

echo "Room # $room is on $floor floor";

?>

Above code is written with proper PHP EOL (semi colon) and would output desired result.

SUPPORT US

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

PHP - Introduction

PHP - Variable Types

Leave A Reply