Friday, August 7, 2009

Anatomy of a PHP Script


Bookmark and Share

  • every PHP script is made up of statements.

  • each statement must be terminated with a semicolon.
    Example:

    some_instruction();
    $variable = 'value';



Note:

The last instruction before a closing tag does not require a semicolon, but it is advisable to always terminate an instruction with a semicolon.

Comments


Types of PHP comments:
  1.  // Single line comment

  2.  # Single line comment

  3.  /* Multi-line
    comment
    */

  4.  /**
    * API Documentation Example
    *
    * @param string $bar
    */
    function foo($bar) { }


Note:

Because the closing tag ?> will also end a comment, code like:
// Do not show this ?> or this 

will output:
or this

  • because the single line comment was terminated by ?>

  • Normally, a single line comment should be terminated with a newline (\r, \n or \r\n)



Whitespace


PHP is whitespace-insentive, except with this few limitations:
  1. You cannot have any whitesapce between <? and php

  2. You cannot break apart keywords
    ex. whi le, fo r, and funct ion

  3. You cannot break apart variable names and function names
    ex. $var name and function foo bar()


Code Block


A series of statements enclosed between two braces:
{
// some comments
f(); // a function call
}

  • used in creating groups of script lines that must all be executed under specific circumstances, such as a function call or a conditional statement.

  • code blocks can be nested.


Language Constructs


Elements that are built-into the language that follow specific rules.
Ex.

echo 10; // will output 10
print (10); // will also output 10
exit(); // terminate the script and either output a string or return a numeric status
die(); // an alias of exit()

Note:

echo is not a function and therefore does not have a return value

Bookmark and Share

No comments:

Post a Comment