PHP

From miki
Revision as of 05:42, 7 January 2016 by Mip (talk | contribs) (→‎Syntax)
Jump to navigation Jump to search

References

Cheat sheets:

Syntax

/* arrays */
$attr = array();          // an empty array
$attr = array("border"=>"myborder", "mode"=>"mymode");   // associative array

/* strings */
'abc' . 'def'                                     // concatenation
"abc $var def"                                    // variable reference in strings
$s=str_replace("\n","",$s);                       // Replace a substring in a string
$s=str_replace(array("\r","\n","\t"," "),"",$s;   // ... idem, with several substrings (also support several "replace" strings)

/* OO */

self::$static_property;
$this->property;
self::CONSTANT;

/* special keywords */
self;              // The current class
$this;             // The current object
NULL;              // The null reference

/* magic constants -- http://php.net/manual/en/language.constants.predefined.php */
__FILE__	   // The full path and filename of the file with symlinks resolved
__DIR__            // Equivalent to dirname(__FILE__)

Unicode support

  • First, if PHP file contains special character, make sure the PHP file is encoded in UTF-8:
file -bi FenTT.php           # To get current encoding
iconv -f $encoding -t UTF-8 -o FenTT.out.php FenTT.php        # Specify correct source $encoding as given above (note that converting us-ascii is useless)
  • Specify the default multi-byte encoding:
mb_internal_encoding("UTF-8");
  • Next store unicode char as follows [1]:
$S = '\u2654\u2655\u2656\u2657';
$S = json_decode('"'.$S.'"');
  • Then access the string using the multi-byte API [2], like [3]:
$D = mb_substr($S,2,1);    // instead of $D=$S[2]

See php reference - string details for more information on how strings are handled internally in php.