Pages

Wednesday, October 2, 2013

Introduction to PHP syntax

Written by: Lorenzo D. Alipio  

What is PHP Syntax? 
PHP Syntax is the format and it contains the set of rules on how PHP program should be written so that it can be interpreted by PHP parser in the web server. Together with the Syntax is another term called Semantics. Semantics is the meanings of symbols, text, characters that can be use in writing PHP program.

What are delimiters in PHP?
Delimiters are symbols that allows the parser to know where the PHP codes begins and where it ends. It is like a set of instructions on how are we going to stack a boxes with a bold printed instruction "This Side UP", or the sign in our local Department of Motor Vehicle  "Line Start Here".

PHP have a commonly use delimiters.
1. The standard tags

      <?php     ?>

between the <?php  and ?>, we can write our PHP variables, functions, classes, and other PHP codes that make up our program.

2. The short hand tags. ( If you are a beginner, I strongly suggest to refrain from using this, until you become familiar with the language. There is nothing wrong using the short hand, but I prefer not using them. It is all a matter of preference).

      <?   ?>

The same as long form, we can write our codes between the opening and the end delimiters.

3. The script tags .

      <script language="php"></script> 

4. ASP like tags. This has to be enabled in the php.ini file. We will be talking about php.ini file at a later date.

    <%   %>

Below are examples of the above tags, please take a look at the codes below and they are all have the same output "Hello World". Assuming that all necessary settings in the php.ini has been enabled.

Standard Tags:

<?php

       echo "Hello World";
?>


Short-hand Tags:

 <?= "Hello World" ?>


Script tags:

<script language="php"> echo "Hello World"; </script>


ASP like tags:

<% echo "Hello World"; %> 


Throughout this tutorial, we will be using the standard tags, because for the reasons I have already mentioned above.

What are the basic PHP language constructs?

1. echo = to print string, value of variables, an output of a function or method. Alternatively, we can use print. Many coders of PHP are debating which one is better echo or print. This is beyond the scope of this tutorial. Yes, these 2 have some minor differences, but it is something we need to be worrying about for now.

2. $ = All variables in PHP must have a dollar sign in-front of them. For example $myVariable

3. Case sensitivity = PHP is  case sensitive. So, for example the following variables are not the same and will be treated by the parsers as three different entities.

$myvariable , $myVariable, $myVariaBle 

4. Assignment operators

  •     =   this is use to make something equal to something. For example, $myVariable = 'Hello World';
  •  => value and key operator . This is use mainly in the array. For example, $myArray = array('name' => 'John', 'last'=>'Doe');

5. Control Structures .

6. Comment tags. In PHP, we have three commonly use tags for commenting within our application.
  • # and //  both tags can be use for inline commenting. e.g.  // this is an inline comment  and # this is an inline comment .
  • /* and */ these tags are use in comment block. Example of usage.. /* this can be a multiple lines of comments or block */ .

This ends the introduction to PHP syntax tutorial. On the next section, we will be writing our first PHP program. Please stand by.

No comments:

Post a Comment