PHP Tutorials: Syntax

Basics

Moving forward with PHP we must know a few important things. This will help us become a good PHP programmer and will also contribute to our earning later. The first thing we should know is everything programming language has Syntax and the language only understand your command if you write the code in a correct Syntax.

Before starting a PHP script you have to prefix “<?php” and at the end of the PHP script you have to suffix “?>”. The PHP script executor will know where your code started and where it ended. See this example.

<?php // Starting
     //Code block
     echo 'Hello World!'; 
?> //Ending

Like most of the programming languages, PHP has “;” (semi-colon) as a statement terminator. When you type “;” the executor knows that the code line ended here and next is going to be a new line of code.

PHP and HTML and embedding story

You can easily embed you can PHP code into HTML. It’s as easy as writing PHP code without HTML. Just start your code by prefixing <?php and suffixing ?> inside any HTML tag and it will work like charm. Here is an example.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>A Simple PHP File</title>
</head>
<body>
    <p><?php echo "Hello World!"; ?></p>
</body>
</html>

In the above code Hello World! will get displayed on the page without the PHP tags. If you will look at the plain HTML by viewing source of the page, you will see the following.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>A Simple PHP File</title>
</head>
<body>
    <p>Hello World!</p>
</body>
</html>

PHP will execute the script and will display the result exactly at the place where you had your PHP script. Wasn’t that easy?

PHP Comments

These are not the comments which you pass on someone. A comment is a single line or a group of lines which is ignored by PHP. A good programmer uses to remember and for future reference or ignore a line of code which you don’t want to execute.

PHP has a single line comment and multiple line comment. For a single line comment, we can use // Double forward slash or # Hash lets take an example.

<?php 
     #I'm single line comment. The code below will output Hello World!
     echo "Hello World!";
     //I'm single line comment. The code above will output Hello World
?>

For multiple line comment start a comment with a slash followed by an asterisk ( /* ) and end with an asterisk followed by a slash ( */ ) let see an example.

<?php 
     /* I am a multiple line comment
     the code below will output Hello World!
     I am sure you get me */
     echo "Hello World!";
?>

Case Sensitivity

Variable names in PHP are case sensitive.

  1. $name
  2. $Name
  3. $NAME

All 3 variable will be treated as different variables and will output the value they contain.

<?php 
     $name = "Naresh Kumar";
     echo $name;
     echo $Name;
     echo $NAME;
?>

$Name and $NAME will output an undefined variable warning. However, the keywords, function name, and class name are case-insensitive. As a result echo and ECHO will produce similar output.

<?php 
     $name = "Naresh Kumar";
     //Both codeline will produce same result
     echo $name;
     ECHO $name;
?>

Go and try the code on localhost.

2 thoughts on “PHP Tutorials: Syntax

Leave a Reply

Your email address will not be published. Required fields are marked *