PHP7 Introduction

PHP Syntax

A php script always starts with <?php and ends with ?> tag.

    // This is a comment in php
    // Additional code goes here

When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them.

Beside <?php and ?> tag php also supports following tags

  • Short open tag <?= ?>
  • Asp Tags  <% %>, <%= ?>
  • Scripting tag: <script language="php">
  • Shorthand tag:  <? ?>

Note: as of php7 asp and scripting tags are removed.

Using short open tag <?= ?> you do not need to type echo. It prints output directly without using echo.

The echo statement can be used with or without parentheses: echo or echo().

Followings are summary of php tags:

<?php echo 'if you want to serve PHP code in XHTML or XML documents, use these tags'; ?>

You can use the short echo tag to <?= 'print this string' ?>. It's always enabled in PHP 5.4 
and later, and is equivalent to <?php echo 'print this string' ?>.

<? echo 'this code is within short tags, but will only work if short_open_tag is enabled'; ?>

<script language="php">
   echo 'this tag is not supported in php7 anymore';

<% echo 'You may optionally use ASP-style tags however they are removed in php7'; %>​

Beside echo function you can also use print function to display contents on browser or console.

The print statement can be used with or without parentheses: print or print().


print "<h2>Sample Header tag</h2>";
print "example with break line<br>";

In, php you can combine multiple strings using . characters. echo function can take mutliple strings as an argument just like a function however print function take single argument.


echo "This will work\n";
echo "argument-1", "argument-2";

print "This will work\n";

// example of combining two string in one using dot
echo "this works" . " and this line is joined\n";
print "this works" . " and this line is joined\n";

// following line wont work
// and throws PHP Parse error
print "argument-1", "argument-2"; 

Note:  echo function can take multiple arguments however print does not. You can concat multiple lines using dot (.) operator.

PHP Statements

To write something on terminal or browser php uses following functions:

  • echo
  • print

Each echo or print statement in php must end with semi colons. If a file is pure PHP code, it is preferable to omit the PHP closing tag (?>) at the end of the file.

This prevents accidental whitespace or new lines being added after the PHP closing tag.

Let's have a look at example:


// first statement
echo "Hello world";

// second statement
echo "Second statement";

echo 'We omitted the last closing tag';

HTML markup can also be added to php echo statement. To print a bold text on browser you can simply do following code:

<?php echo "<b>This is bold text</b>"; ?>

Everything out side php tag is considered as html content because php is embeded inside html language.

For example,

    <?php echo "<h1>Sample header tag</h1>"; ?>


PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments. Comments can be of different types:

  • Single line comments starts with either // or #
  • Multi-line comments starts with /* and ends with */

Comments are not shown on console or browser because they act as developer guidelines.

Let's have a look at some examples of comments:


// This is a single line comment

# This is also single line comment

    This is an example of multi-line comment
    You can add as many lines you want

// Author: Sandip Patel

Note: avoid nested comments to prevent fatal errors

Interview Questions:

  • What is the correct and the most two common way to start and finish a PHP block of code?
    • <?php // code goes here ?>
    • <?  // code goes here ?>
  • How can we display the output directly to the browser?
    • to display the output directly to the browser, we have to use the special tags <?= and ?>
  • What are the different ways to write single line comments in php?
    • // or #
  • What are the ways you can declare multi line coments in php?
    • /* this is multi-line comment */
  • Which php tags are deprecated in php7?
    • asp Tags  <% %>, <%= ?>
    • scripting tag: <script language="php">
  • What is the difference between echo and print functions?
    • echo takes multiple arguments however print does not
  • How do you combine two strings in php?
    • You can easily combine or concat two strings using dot (.) operator.
  • What are the different ways to output a string in php?
    • Using print or echo you can output strings in php