Wednesday, September 16, 2020

PHP Basic Syntax



Hello dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about the PHP Syntax.

This tutorial will give you an idea of the basic syntax of PHP, and its very important to make your foundation in PHP very strong.

Escaping to PHP

The PHP parsing engine needs a way to differentiate PHP code from other elements in the page. The process for doing so is known as 'escaping to PHP'. There are four ways to do this -

1. Cononical PHP Tags

The most universal efficient PHP tag style is -

<?php...?>

If you make use of this style, you can be positive that your tags will always be correctly interpreted.


2. Short-open (SGML-style) Tags

Short-open tags look like this -

<?...?>

The short tags are, as one might expect, the shortest option. You need to do one of the two things to enable PHP recognize the tags -

  • Select the --enable-short-tags config option when you are building PHP.
  • You also have to set the short_open_tag option in the php.ini file to On. This option needs to be Off in order to be able to parse XML with PHP because the same syntax is used for XML tags.

3. ASP-style Tags

ASP-style tags imitate the tags used by Active Server Pages to describe code blocks. ASP-style tags look like this -

<%...%>

To use ASP-style tags, you will need to set the configuration option in the php.ini file.


4. HTML Script Tags

HTML script tags look like this -

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


Commenting PHP Code

A comment is the portion of a program that exists only for human readers and stripped out before showing the output of the program. There are two formats of commenting in PHP -

Single-Line Comments

Single-line comments are normally used for short explanations or notes relevant to the code. The following below are the examples of single line comments -

<?
   # 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-Lines Printing

Following below are the examples to print multiple lines in a single line statement -

<?
   # 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-Lines Comments

Multi-lines comments are normally used for providing more detailed explanations when necessary. The multi-line style of commenting is the same as in C. Here are the examples of multi-lines comments -

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

PHP is Whitespace Insensitive

Whitespace is the stuff you type that is typically invincible on the screen, including spaces, tabs, and carriage returns.

Whitespace insensitivity means that its almost never important how many of the whitespace characters you have in a row. One whitespace character is the same as so many of such characters.

Example

Each of the following below PHP statement that assigns the sum of 2 + 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

PHP is a case sensitive language.

Example

Try out the following example below -

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

Output

When the above code is executed, it will produce the following result -

Variable capital is 80
Variable CaPiTaL is

Statements are Expressions that are Terminated by Semicolons

A statement in PHP language is an expression that is followed by a semicolon (;). Any order of valid PHP statements that is enclosed by PHP tags is a valid program.

Example

Following is a typical statement in PHP, which in this case assigns a string of characters to a variable that is called $greeting -

$greeting = "Welcome to PHP!";


Expressions are Combinations of Tokens
The smallest building blocks of PHP are the unbreakable tokens, such as numbers (6.141), strings (.one.), variables ($one), constants (TRUE), and the special words that make up the syntax of PHP itself like if, else, while, for, etc.

Braces make Blocks
Although statement can not be merged like expressions, you can always put an order of statements anywhere a statement can go by enclosing them in a set of curly braces.

Example
Here both statements are equal -

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
You can run your PHP script on your command prompt. Assume you have the following content in test.php file -

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

Now run the PHP script from a command prompt as follows -

$ php test.php

Output
When the above code is executed, it will produce the following result -

Hello PHP!!!!!


Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we are going to be studying about the PHP Variable Types.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.