PHP Examples
<?php // php comments /* More comments */ $var = "hello world" print $var." from php"; echo "{$var}, like shell"; define("MY_PI", 3.14159); print MY_PI; if($x > $y){ $max = $x; }else { $max = $y; } $count = $result = 1; while($count <= $N){ $result *= $count; $count++; } for($i = 0; $i < 100; $i++) { $result += $i; } /* type converting */ string strval(mixed var); integer intval(mixed var [, integer base]); float floatval(mixed var); boolean settype(mixed var, string type); /* type checking */ boolean is_int(mixed var); boolean is_float(mixed var); boolean is_bool(mixed var); boolean is_string(mixed var); boolean is_array(mixed var); boolean is_object(mixed var); /* debug */ string gettype(mixed var); print_r(mixed expr); var_dump(mixed expr [, mixed expr]); /* test setting */ boolean isset(mixed var); boolean empty(mixed var); /* user defined function */ function func($arg1 = 0, &$arg2) { global $temp; static $count = 0; } func($a, $b); include "functions.inc"; require "/DB.php"; #relative to include_path defined in php.ini $numbers = array(5,6,7,8,9); $shopping = array(); $shopping[] = "milk"; $shopping[] = "coffee"; $shopping[] = "sugar"; $mapping = array("first" => 1, "second" => 2, "third" => 3); $mapping["first"] = 0; unset($mapping); // destory a whole array $planets = array( "Mecury" => array("dist" => 0.39, "dia" => 0.38), "Venus" => array("dist" => 0.72, "dia" => 0.95), "Earth" => array("dist" => 1.00, "dia" => 1.00, "moon" => array("Moon")), "Mars" => array("dist" => 0.39, "dia" => 0.53, "moon" => array("Phobos", "Deimos")), ); print $planets["Earth"]["moon"][0]; /* foreach with array */ foreach($mapping as $number) { } foreach($planets as $name => $info) { } // array functions integer count(mixed var); array array_count_values(array input); #count every value's frequence array array_fill(integer start, integer count, mixed val); array range(mixed low, mixed high [, integer step]); array explode(string separator, string subject[, integer limit]); // break string into array at each occurence of the separator string string implode(string glue, array pieces); // join each element in pieces, inserting glue between each piece number max(array numbers); number min(array numbers); boolean in_array(mixed needle, array haystack [, boolean strict]); mixed array_search(mixed needle, array haystack [, boolean strict]); // return the key of the matching value or false boolean array_key_exists(mixed key, array source); array array_keys(array input [, mixed search_value]); // return all keys or find keys for a particular value array array_values(array input); array array_unique(array values); array array_merge(array array1, array array2 [, array ...]); array array_reverse(array source [, bool preserve_keys]); sort(array subject [, integer sort_flag]); // SORT_STRING, SORT_REGULAR rsort(array subject [, integer sort_flag]); // descending order asort(array subject [, integer sort_flag]); // sort associative array on its values arsort(array subject [, integer sort_flag]); usort(array subject, string compare_function); // user defined sort: integer compare_function(mixed a, mixed b); uasort(array subject, string compare_function); uksort(array subject, string compare_function); // based on key /* string functions */ integer strlen(string subject); // formatting string string sprintf(string format [, mixed args]); // %b %c %d %u %o %xX %f %s integer printf(string format [, mixed args]); // string padding string str_pad(string input, int length [, string padding [, int pad_type]]); // STR_PAD_LEFT, STR_PAD_BOTH // change case string strtolower(string subject); string strtoupper(string subject); string ucfirst(string subject); string ucwords(string subject); // trim whitespace string ltrim(string subject [, string character_list]); string rtrim(string subject [, string character_list]); string trim(string subject [, string character_list]); // compare string integer strcmp(string str1, string str2); integer strcmp(string str1, string str2, integer length); // substring string substr(string source, integer start [, integer length]); integer strpos(string haystack, string needle [, integer offset]); // search occurs from offset integer strrpos(string haystack, string needle); // return last occurence of the needle string strstr(string haystack, string needle); // return the portion of haystack from the first occurence of needle to the end of haystack string stristr(string haystack, string needle); // case insensitive string substr_replace(string source, string replace, int start [, int length]); mixed str_replace(mixed search, mixed replace, mixed subject); // translate string strtr(string subject, string from, string to); string strtr(string subject, array map); // regular expression functions boolean ereg(string pattern, string subject [, array var]); // var is populated with the portions of subject that are matched by pattern boolean eregi(string pattern, string subject [, array var]); // case-insensitive string ereg_replace(string pattern, string replacement, string souce); string eregi_replace(string pattern, string replacement, string souce); array split(string pattern, string source [, int limit]); array spliti(string pattern, string source [, int limit]); // date and time integer time(); // return timestamp since 1970 int mktime(int hour, int minute, int second, int month, int day, int year [, int is_dst]); // create timestamp for a date integer strtotime(string time); string microtime(); // begin with microsecond, followed by integer timestamp string date(string format [, int timestamp]); boolean checkdate(int month, int day, int year); // int and float mixed abs(mixed number); float round(float val [, int precision]); // number systems string decbin(int number); int bindec(string binarystring); string dechex(int number); int hexdec(string hecstring); string decoct(int number); int octdec(string octalstring); // OOP class CaseCounter extends UnitCounter { private $unitsPerCase; var $caseCapacity = 1.0; protected function addCase() { $this->add($this->unitsPerCase); } function __construct($unitWeight) { parent::__construct($unitWeight); $this->unitsPerCase = $caseCapacity; } } ?>