PHP 7 Code Syntax

For learning PHP, you should have basic knowledge of HTML and CSS.

PHP is a Hybrid language. It has both procedural and object oriented features. It takes most of the syntax from other language such as C, Perl, Shell, Java. PHP Syntax is very simple and easy to use. We can write PHP code in an HTML file.

Types of PHP tags -

PHP Standard Tags
<?php 
....
?>
These are more compatible, portable and mostly used tags.
PHP Short Tags
<?
.....
?>
It require shorthand tags or short_open_tag to be ON on php.ini setting. We generally avoid to use these tags because this create a problem in XML parsing. As XML has the same syntax, so they has a major drawback of conflicting with XML headers.
PHP Script Tags
<script language="php">
.....
</script>
These tags are removed from PHP 7. These are less portable and generally not recommended.
PHP ASP Tags
<%
.....
%>
It requires to ON the asp tags in the php.ini configuration.

All the PHP Code are written between the start and end tags. PHP processor parses the code between these tags. Everything outside of these tags are ignored by the PHP Parser. We can ignore the closing tag if the file contains only PHP Code.



PHP File Extension

PHP file extension is ".PHP".

Example - index.php, introduction.php, aboutus.php

Basic Constructs

  • Like other languages, PHP script is made up of statements, variables, functions, loop structure and so on.
  • Each PHP statement is terminated with a semicolon.
    Like- echo 'Hello World';
  • PHP use '=' as the assignment operator.
    Like- $var = 'Dog';
  • PHP displays a statement by using ECHO and PRINT.
    Like - echo $var;
    print "Hello World";
  • PHP code ignores whitespace between statements.

Adding PHP to HTML

This is our basic first program. Open a new file in your preferred editor and type -
<html>
    <head>
        <title>PHP Basic Example</title>
    </head>
    <body>
        <?php
            echo 'Hello World';
        ?>
    </body>
</html>

Let us save the above script as sample.php and start the server installed on your machine, suppose the webserver is running at -

http://localhost/sample.php

Output of the above code is - Hello World.

PHP can be embedded inside HTML. Everything compatible with HTML is also compatible with PHP at the client side. Like - Javascript, CSS. PHP parser starts reading the PHP from the PHP start tag and stops at the PHP close tag. Everything within these open and close tags are understood by the PHP Parser.




Read more articles


General Knowledge



Learn Popular Language