.:An Endless Journey:.
open source and me
  • Immutable Page
  • Info
  • Attachments

PHP Syntax quick refresher

General

Even you are writing GUI apps or script, you still need '<?php'

  <?php              // structure
    //               // one line comment
    /* */            // block comment
    echo ("hello");  // ';' to end statement
  ?>

Typing in PHP

Weakly typed -- type of object are determined by context

  $aStr = "string";         // "string" indicate "aStr" is of "string" type

Valid types:

    boolean   : true, false   // NOTE: true/false are \Case Insensitive\
    integer   :  signed integer, 1234, 0755, 0xFF
    float     :  0.11, or 1.234e-11
    string    :  ''           // only valid: \\, \'
                 ""           // \n, \r, \r, \\, \$, \", \[octal], \[hex]
                 <<<TOK
                 multiline string...
                 TOK;

Strings

  concate :  "prefix" . "something" . "suffix"
  or, $aStr .= "some suffix"   // concate + assignment
  In comparsion,
   ""  -> FALSE
   "0" -> FALSE

  Variable in string:
    $aVar
    $aArrayElement[index]

Constants

  define ('CONSTANT_NAME', 'CONSTANT_VALUE');
  define()
  defined()
  constant()
  get_defined_constants()
  [Magic Constants]
  - defined by PHP, and the value is changed depends on where you use it
  __LINE__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__ (PHP5 only)

Very common function (__builtins__)

  echo,
  print
  error_reporting
  strval
  print_r
  var_dump

File

  fopen,fread,fclose
  $fileContents = file(), foreach ($fileContents as $line)
  $allData = file_get_contents("path/to/file")
  file_exists(), filesize(), etc
  is_dir(), is_links, etc

OO

 class Programmer {
   public $name;
   public $email;

   public function sick() {
     echo $this->name . " is sick\n";
   }
 }

 $littleJacky = new Programmer;