三日php之路 -- 第一天(php語言參考)

1、基本語法php

    (1)PHP標記html

<?php

echo "Hello World!";

// 當文件爲純PHP時,最好在末尾刪除PHP結束標記
//?>

    (2)從HTML中分離express

// 在一對開始和結束以外的內容,都會被PHP解釋器忽略。也就是html標籤和PHP代碼混合的那種,跟jsp,asp同樣...
<p>This is going to be ignored by PHP and displayed by the browser.</p>
<?php echo 'While this is going to be parsed.'; ?>
<p>This will also be ignored by PHP and displayed by the browser.</p>

// 使用條件,高級分離
<?php if ($expression == true): ?>
    This will show if the expression is true.
<?php else: ?>
    Otherwise this will show.
<?php endif; ?>

    (3)指令分隔符,註釋數組

        PHP須要在每一個語句後面用分隔符結束指令。jsp

        註釋: // 或 /* ... */  可是,*/ 會匹配最近的那個,切記!切記!函數


2、類型this

    PHP支持8種原始數據類型。spa

    四種標量類型:boolean(布爾型),integer(整型),float(浮點型,double),string(字符串)code

    兩種複合類型:array(數組),object(對象)htm

    兩種特殊類型:resource(資源),NULL(無類型)

<?php
$a_bool = TRUE;   // a boolean
$a_str  = "foo";  // a string
$a_str2 = 'foo';  // a string
$an_int = 12;     // an integer

echo gettype($a_bool); // prints out:  boolean
echo gettype($a_str);  // prints out:  string

// If this is an integer, increment it by four
if (is_int($an_int)) {
    $an_int += 4;
}

// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
    echo "String: $a_bool";
}
?>

    (1)Boolean 布爾類型

        能夠爲TRUE或FALSE,不區分大小寫。

        通常非0,即爲TRUE。

    (2)Integer 整型

        整型可使用十進制,十六進制,八進制或二進制表示。八進制前面必須加0(零),十六進制加0x,二進制加0b。

        若是給定的一個數超出了interger的範圍,將會被解釋爲float。一樣運算結果超出integer範圍,一樣如此。

        php沒有整除運算符,1/2 將產生出 float 0.5。能夠強制轉換爲integer 或使用round() 更好的四捨五入。

echo (int)2.9; // 輸出 2
echo round(2.555, 2) // 輸出 2.56
// 決不要將未知的分數強制轉換爲 integer,這樣有時會致使不可預料的結果。
<?php
echo (int) ( (0.1+0.7) * 10 ); // 顯示 7!
?>

    (3)Float 浮點型(double)

        浮點型,也叫浮點數float,雙精度double, 實數real。

<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

    (4)String 字符轉

        一個字符串string,就是由一系列的字符組成,其中每一個字符等同於一個字節。這就意味着php只能支持256個字符集,所以不支持Unicode。

        string最大能夠達到2GB。

<?php
$a = 123;
echo '$a'; // 輸出 $a
echo "$a"; // 輸出 123, 轉義字符 '\'

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;

?>

    (5)Array 數組

        php中的數組,其實是一個有序序列。映射是把values關聯到keys的類型。

        因爲數組元素的值也能夠說是另外的數組,樹形結構和多維數組也是容許的。

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// 自PHP 5.4 起
$array = [
    "foo" => "bar",
    "bar" => "foo",
]
// key 能夠是 integer 或 string 類型
// key 值爲可選項, 若是未指定,則使用以前用過最大的integer鍵名加上1做爲新鍵名
?>

// 要修改某個值,經過其鍵名給該單元賦一個新值。
// 要刪除某個鍵值對,對其調用 unset() 函數。

        使用 unset() 須要注意,此時數組不會重建索引。須要重建索引,可使用 array_values() 函數。

        數組計算總數: 使用 count() 函數

    (6)Object 對象

<?php
class foo{
    function do_foo(){
        echo "Doing foo.";
    }
}
// 用 new 實例化一個類
$f = new foo;
$f->do_foo;

    (7)Resource 資源類型

        資源 resource 是一種特殊的變量,保存了到外部資源的一個引用。資源是經過專門的函數來創建和使用的。

    (8)NULL

        特殊的NULL 表示一個變量沒有值。NULL類型惟一可能的值就是NULL。

        可被認定爲NULL的變量:①被賦值爲NULL ②還沒有被賦值 ③被unset

    (9)Callback 回調類型

        自PHP5.4 起,可使用 callable 類型 指定回調類型 callback。


3、變量

    php中變量用一個美圓符號 $ 後面跟變量名來表示的。區分大小寫。

    變量默認老是傳值賦值。

<?php

$a = 1;
// 值傳遞賦值 
$b = $a
// 引用賦值
$c = &$a

// global 關鍵字
global ; $GLOBALS
相關文章
相關標籤/搜索