Forum und email

文句

指定變數為文句形態有幾種語法可以用。

If the string is enclosed in double-quotes ("), variables within 如果文句是用雙引號包起來的話, 含在其中的變數會被展開 (當然也受分析器分析時的限制)。 和 C、 PERL 一樣, 想加入特別的控制符可以前置 "\"符號:

特別控制符號串
符號串 含意
\n 開新一行 (LF or 0x0A in ASCII)
\r 斷行 (CR or 0x0D in ASCII)
\t 跳格 (HT or 0x09 in ASCII)
\\ backslash 反斜線
\$ $ 符號
\" 雙引號
\[0-7]{1,3} 8 進制的字符 (左面用的是 REGULAR EXPRESSION 的表達方法‧)
\x[0-9A-Fa-f]{1,2} 16 進制的字符

你可以在 "\" 後放上任何別的字元, 但是在程式執行時會有警告。(除非另外調設, 否則警告不會影響程式執行。

第二種定義文句的方法的方法是用單引號 "'"。 在單引號文句中只有兩個按制項: "\\" 和 "\'", 這是為了能在文句中加入單引號。 單引號文句中的變數並不會 被展開。

另外還有一種界定文句的方法: 用 HERE DOC 的句式 ("<<<")。 注意要在 <<<之後加上一個辨識字並必須在文句完後的第一行 最開頭 加上同一個辨識字。

The closing identifier must begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Here doc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a here doc as with strings.

Example#1 用 Here doc 界定文句的示範

<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

/* More complex example, with variables. */
class foo {
    var $foo;
    var $bar;

    function foo() {
        $this->foo = 'Foo';
        $this->bar = array('Bar1', 'Bar2', 'Bar3');
    }
}

$foo = new foo();
$name = 'MyName';

echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
     

Note: 在 PHP 版 4 中才能用 Here doc 句法.

文句可以用點 '.' 串接起來。注意用 "+" 號串連文句是無效的。 文句運算符 一節中有更詳細的紀錄。

在文句中的每一個字母可以當是字母陣列中的一個個元素, 如此可以用像 C 那樣的語法來調用, 請看下例 :

Example#2 示範文句

<?php
/* Assigning a string. */
$str "This is a string";

/* Appending to it. */
$str $str " with some more text";

/* Another way to append, includes an escaped newline. */
$str .= " and a newline at the end.\n";

/* This string will end up being '<p>Number: 9</p>' */
$num 9;
$str "<p>Number: $num</p>";

/* This one will be '<p>Number: $num</p>' */
$num 9;
$str '<p>Number: $num</p>';

/* Get the first character of a string  */
$str 'This is a test.';
$first $str[0];

/* Get the last character of a string. */
$str 'This is still a test.';
$last $str[strlen($str)-1];
?>

文句變換

當文句被當成是數字那樣來操作時, 其形態和值跟從以下的規則:

如果文句中有 '.'、 'e'、 'E'符號, 則文句會變成浮點數字, 反之則會被當成是整數。

文句所代的數值視文句開頭的字母而定, 只要文句開頭的不是合理的數字符號則文句的值就會是 0。 合理的數字符號包括正負號、 後面跟 0 到 9 的阿拉伯數字、 小數點、 最後是可有可無的指數代表符號 ("E" 或 "e" 後面跟上一串阿拉伯數字)。

如果在表達示中頭一項是文句則運算結果要看第二項是什麼變數形態。

$foo = 1 + "10.5";              // $foo is double (11.5)
$foo = 1 + "-1.3e3";            // $foo is double (-1299)
$foo = 1 + "bob-1.3e3";         // $foo is integer (1)
$foo = 1 + "bob3";              // $foo is integer (1)
$foo = 1 + "10 Small Pigs";     // $foo is integer (11)
$foo = 1 + "10 Little Piggies"; // $foo is integer (11)
$foo = "10.0 pigs " + 1;        // $foo is integer (11)
$foo = "10.0 pigs " + 1.0;      // $foo is double (11)

在 Unix 手冊 strtod(3) 一條中有更關於這類變換的資料。

如果要試用本節中示範的例子, 可以把程式碼複製到一個新的程式段中並插入下列指令行, 它告訴你使用中的變數是什麼形態 :

echo "\$foo==$foo; type is " . gettype ($foo) . "<br>\n";