February 25, 2009

PHP Basics : Part I

Talking about the basics of a programming language the things that come into minds at a glance may be --
  • Syntax,
  • Language Construct,
  • Data Types,
  • Variables,
  • Constants,
  • Operators,
  • Control Structure,
  • Errors and Errors Management, etc.

Syntax: The syntax of php is taken from many common language, most dominantly from C and then Perl. Now as the object oriented system is introduced, java like syntax is adjusted too.

>>Tags -- Php is used as pure programming language. But most often this can be used as
text processor or scripting language. For this, php code can be inserted directly into text or
markup languages using special sets of tags. Tags are to indicate starting and ending of a
code block. They can be like -
Standard Tags
<?php echo $code;?>

Short Tags


Script Tags

<script language="“php”">

... code


Here among the tag conventions, standard tags are best too use. Because they are available
always and by default. And there need no extra configuration to enable them. Short tags
have some advantage like - they can be used to print the result of an expression directly to
the script output. But they have major disadvantages like, the tags may be conflicting with
those of xml headers. Script tags are used in HTML like javascript is used, as so many
browsers and editors just ignored javascipt and php standard codes.

There is another tag called Asp tag. Starting with "". No specific
reason is found why this tag was introduced.

But using short tags, script tags and asp tags are not counted among "GOOD" programming

>>Newline Character -- Every character outside the php tags are interpreted as-is. So
there comes a new line character at the finising of the php tag. But newline character is used
as sperator between web servers HTTP request and actual data. So there raises some
conflicts regarding that issue. So the first new line after the closing tag of php is handled by
the parser. But to avoid conflict or errors, there is an easy way to omit the closing tag (?>) .
That is not risky because the parser allows this as perfectly legal.

>>Comments -- Comments is an important and common part of any programming
language. Php supports a variety of commenting style and pattern.
They can be given as follows -
  • // Single line comment
  • # Single line comment
  • /* Multi-line
  • /**
* API Documentation Example
* @param string $param
function f($param) { }

Both the types of single line commenting should be ended by newline (\r or \n or \r\n) or
by ending current php code block tag. But the later one is not encouraged to use as tehy can
create unexpected result.

>>White Spaces Rules -- PHP is whitespace-insensitive, other than in some key areas.
Actually there are no requirement to use some whitespace characters but there are some
limitations instead. They can be stated as:
• No whitespace between < ? and php
• No whitespace between keywords (e.g.: whi le, fo r, and funct ion)
• No whitespace between variable names and function names, (e.g.: $var name
and function foo bar())


No comments:

Post a Comment