PHP – Syntax Overview

 

PHP – Syntax Overview,This bankruptcy will give you an concept of very simple syntax of PHP and very critical to make your PHP foundation robust.

Escaping to PHP The PHP parsing engine desires a manner to distinguish PHP code from other factors inside the page. The mechanism for doing so is known as ‘escaping to PHP’. There are 4 approaches to try this −

Canonical PHP tags The most universally effective PHP tag fashion is −

<?php...?>

If you use this style, you could be positive that your tags will usually be efficaciously interpreted.

PHP – Syntax Overview,Short-open (SGML-style) tags

Short or short-open tags look like this −

<?...?>

Short tags are, as one may count on, the shortest option You should do certainly one of two matters to permit PHP to recognize the tags −

  • Choose the –allow-brief-tags configuration alternative while you’re constructing PHP.
  • Set the short_open_tag setting in your php.Ini document to on. This choice need to be disabled to parse XML with PHP due to the fact the same syntax is used for XML tags.

PHP – Syntax Overview,ASP-style tags

ASP-style tags mimic the tags used by Active Server Pages to delineate code blocks. ASP-style tags seem like this −

<%...%>

To use ASP-style tags, you may need to set the configuration alternative on your personal home page.Ini record.

PHP – Syntax Overview,HTML script tags

HTML script tags look like this

<script language = "PHP">...</script>

PHP – Syntax Overview,Commenting PHP Code

A remark is the portion of a application that exists handiest for the human reader and stripped out earlier than displaying the packages result. There are two commenting codecs in PHP −

Single-line feedback − They are normally used for brief causes or notes applicable to the local code. Here are the examples of unmarried line feedback.

<?
   # 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";
?>

Multi-strains printing − Here are the examples to print multiple strains in a unmarried print announcement −

<?
   # First Example
   print <<<END
   This uses the "here document" syntax to output
   multiple lines with $variable interpolation. Note
   that the here document terminator must appear on a
   line with just a semicolon no extra whitespace!
   END;
   
   # Second Example
   print "This spans
   multiple lines. The newlines will be
   output as well";
?>

Multi-strains comments − They are generally used to provide pseudocode algorithms and greater specific factors while important. The multiline style of commenting is the same as in C. Here are the instance of multi traces comments.

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

PHP is whitespace insensitive

Whitespace is the stuff you type this is typically invisible on the screen, consisting of spaces, tabs, and carriage returns (cease-of-line characters).

PHP whitespace insensitive means that it almost in no way topics what number of whitespace characters you’ve got in a row.One whitespace person is the same as many such characters.

For instance, each of the subsequent PHP statements that assigns the sum of two + 2 to the variable $four is equivalent −

$four = 2 + 2; // single spaces
$four <tab>=<tab2<tab>+<tab>2 ; // spaces and tabs
$four =
2+
2; // multiple lines

PHP is case sensitive

Yeah it is true that PHP is a case sensitive language. Try out following example

<html>
   <body>
      
      <?php
         $capital = 67;
         print("Variable capital is $capital<br>");
         print("Variable CaPiTaL is $CaPiTaL<br>");
      ?>
      
   </body>
</html>

This will produce the following result −

Variable capital is 67
Variable CaPiTaL is

Statements are expressions terminated by semicolons

A announcement in PHP is any expression that is observed by way of a semicolon (;).Any sequence of legitimate PHP statements that is enclosed by way of the PHP tags is a legitimate PHP software. Here is a regular announcement in PHP, which in this situation assigns a string of characters to a variable called $greeting −

$greeting = "Welcome to PHP!";

Expressions are combinations of tokens

The smallest constructing blocks of PHP are the indivisible tokens, along with numbers (three.14159), strings (..), variables ($), constants (TRUE), and the special words that make up the syntax of PHP itself like if, else, while, for and so on

Braces make blocks

Although statements cannot be combined like expressions, you could always placed a sequence of statements everywhere a announcement can move by way of enclosing them in a fixed of curly braces.

Here both statements are equivalent −


if (3 == 2 + 1)
   print("Good - I haven't totally lost my mind.<br>");
   
if (3 == 2 + 1) {
   print("Good - I haven't totally");
   print("lost my mind.<br>");
}

Running PHP Script from Command Prompt

Yes you could run your PHP script to your command set off. Assuming you have following content material in take a look at.Php record

<?php
   echo "Hello PHP!!!!!";
?>

$ Hypertext Preprocessor test.Hypertext Preprocessor It will produce the subsequent end result −

Hello PHP!!!!!