PHP - SPLessons

PHP Syntax

Chapter 4

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Syntax

PHP Syntax

Syntax features of PHP

shape Introduction

  • PHP is forgiving: Certain syntactical features like function prototypes and variable declarations are not needed in PHP. It stresses accommodation for the developer over accuracy.It has least set of syntactical principles that the code must take after.If any parse errors are found in the browser window, then it means PHP rules are not followed.
  • HTML is not PHP : HTML is a static library but PHP is a dynamic language.They can interact with each other but they are not the same.PHP syntax is relevant only with PHP since PHP is implanted in HTML components.All aspects of a report is interpreted as either PHP or HTML,depending on the labels.
  • PHP syntax looks like C-language : Loops,control structures also exist in PHP.
  • PHP is case sensitive : Once a variable declared in a case,it cannot be changed.Statements are terminated with semicolons.

shape Description

PHP Parsing engine finds out and differentiate the PHP code by identifying the code having the following 4 tags.

Canonical tags

PHP script starts with <?php and ends with ?>

Syntax:

<?php write your php code here ?>

Short-open (SGML-style) tags

Before using these tags,configuration has to be done in the editor.The php script with this tags start with <? and end with ?>.

Syntax:

<?Write your code here?>

ASP-style tags

These tags are normally used by Server Pages that are active to delineate code blocks. The php script with this tags start with <% and end with %>.

Syntax:

<%Write your code here%>

HTML script tags

The script tags of HTML can be used to define PHP.The php script with this tags start with <script> and end with </script>.

shape Syntax
Explanation

shape More Info

Basically PHP file contains normal HTML elements and PHP scripts.PHP script can be kept anywhere in the document, but the file has to be saved with .php extension, otherwise PHP script will be considered as normal HTML text.

Every PHP statement ends with a semicolon;.All these statements can be enclosed within block that start and end with curly braces.{}.

shape Example

<!DOCTYPE html>
<html>
	<body>
		<h1> Hello World ! Welcome to Splessons </h1>
		<?php echo " Hello World, Welcome to Splessons"; ?>
	</body>
</html>

Output:

PHP Case Sensitivity

shape Description

All the classes, functions, user defined functions and keywords using in the PHP are not case sensitive, but variables created in PHP are case sensitive.

Comments In PHP

shape Description

Comments in the code level, helps to write description to the module for better understanding the coding flow and the comments part will not execute, it is just for reading purpose regarding the module.

Types of comments:

shape Example

Below is the basic example of php comments

<!DOCTYPE html>
<html>
<body>

<?
   # This is a comment, and
   # This is the second line of the comment
   
   // This is a comment too. Each style comments only
   print "An example with single line comments";
?>

<?
   /* This is a comment with multiline
      Author : John
      Purpose: Multiline Comments Demo
      Subject: PHP
   */
   
   print "An example with multi line comments";
?>

</body>
</html>

Output:

Basic Example

shape Example

<!DOCTYPE html>
<html>
    <head>
      <title>Splessons</title>
    </head>
     
    <body>
        <?php echo "Hello World!!, Welcome to Splessons"; ?>
    </body>
</html>

Output:

Summary

shape Key Points

  • PHP script starts with <?php and Ends with ?>.
  • PHP is case-sensitive.
  • PHP file ends with and extension .php.
  • Single and multi-line comments exists in PHP.