PHP 常用語句
extension_loaded 判斷是否有載入模組
bool extension_loaded ( string $name )
拆解字串為二維陣列 及 mongo座標搜尋
array_map (
function ($_){return explode (',', $_);},
explode ('_', $arrayStr )
);
array(
'$geoWithin' => array(
'$polygon' => $latlonPolygon
)
)
正規表示 +1
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
/*
Result Array
(
[0] => Array
(
[0] => foobarbaz
[1] => 0
)
[1] => Array
(
[0] => foo
[1] => 0
)
[2] => Array
(
[0] => bar
[1] => 3
)
[3] => Array
(
[0] => baz
[1] => 6
)
)
*/
in_array +2
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
is_array +1
bool is_array ( mixed $var )
die +1
//die()停止程序运行,输出内容
die ("Access Denied");
php程式碼規範 +1
http://blog.kejyun.com/2013/05/Readable-Coding-Style.html
http://blog.webgolds.com/view/230
注解上面要空白一行
代碼必須使用4個空格符而不是 tab鍵 進行縮進。
邏輯符號 > < = 左右空白一格
class 命名大駝
function 命名小駝
變數命名 小寫英數 或 _
常數命名 大寫英數 或 _ (定義在同一檔案內)
php keyword 一律用小寫
請用and或or 取代 && || 程式更容易閱讀
物件內部變數請使用private , protected , public
. 屬性
每個屬性都必須添加訪問修飾符。
一定不可使用關鍵字 var 聲明一個屬性。
每條語句一定不可定義超過一個屬性。
不要使用下劃線作為前綴,來區分屬性是 protected 或 private。
方法名或函數名與參數左括號之間一定不能有空格,參數右括號前也 一定不能有空格。每個參數前一定不能有空格,但其後必須有一個空格。
switch
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
implode +1
Join array elements with a string:
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
explode +2
$str = "Hello world. I love Shanghai!";
print_r (explode(" ",$str));
strrpos +2
strrpos($mystring, "b");
foreach +1
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
建立日期 +1
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime("now");
DateInterval 日期時間差異比較 +1
http://php.net/manual/en/class.dateinterval.php
$d1 = new DateTime();
$d2 = new DateTime();
$d2->add(new DateInterval('PT'.$timespan.'S'));
$iv = $d2->diff($d1);
print_r($iv);
$iv->days; //差異天數
取某個日期的 這週第一天最後一天、這個月第一天最後一天
//date time
echo "\nDatetime diff\n";
$datetime1 = strtotime('2009-10-11');
$datetime2 = new DateTime("now");
//$interval = $datetime1->diff($datetime2);
//echo "datetime1 datetime2時間差:". $interval->days ."天\n";
echo "--week 日期計--\n";
$date = new DateTime("2017-12-27");
$date->modify('monday this week');
echo $date->format("Y-m-d") ."\n";
// $date->modify('monday next week');
// echo $date->format("Y-m-d") ."\n";
$date->modify('first day of +0 month');
echo $date->format("Y-m-d") ."\n";
$date->modify('last day of +0 month');
echo $date->format("Y-m-d") ."\n";
//echo strtotime("this monday");
preg_split +1
利用正規表示式,切割一個字串
array preg_split ( string $pattern , string $subject , int $limit , int $flags )
substr("Hello world",6) +1
str_replace(find,replace,string,count) +1
gettype() +1
Get the type of a variable
string gettype ( mixed $var )
list() +1
把数组中的值赋给一些变量:
<?php
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>
is_numeric() +1
判斷是否是數字
strtolower() +1
var 分大小寫 function class不分大小寫