PHP 大小寫敏感
在 PHP 中,所有用戶定義的函數(shù)、類和關(guān)鍵詞(例如 if、else、echo 等等)都對大小寫不敏感。
在下面的例子中,所有這三天 echo 語句都是合法的(等價):
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
不過在 PHP 中,所有變量都對大小寫敏感。
在下面的例子中,只有第一條語句會顯示 $color 變量的值(這是因為 $color、$COLOR 以及 $coLOR 被視作三個不同的變量):
<!DOCTYPE html>
<html>
<body>
<?php
$color="red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
PHP global 關(guān)鍵詞
global 關(guān)鍵詞用于訪問函數(shù)內(nèi)的全局變量。
要做到這一點,請在(函數(shù)內(nèi)部)變量前面使用 global 關(guān)鍵詞:
<?php
$x=5;
$y=10;
function myTest() {
global $x,$y;
$y=$x+$y;
}
myTest(); // 運行函數(shù)
echo $y; // 輸出變量 $y 的新值
?>
PHP echo 和 print 語句
echo 和 print 之間的差異:
echo - 能夠輸出一個以上的字符串
print - 只能輸出一個字符串,并始終返回 1
提示:echo 比 print 稍快,因為它不返回任何值。
<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";
?>
顯示變量
下面的例子展示如何用 echo 命令來顯示字符串和變量:
<?php
$txt1="Learn PHP";
$txt2="W3School.com.cn";
$cars=array("Volvo","BMW","SAAB");
echo $txt1;
echo "<br>";
echo "Study PHP at $txt2";
echo "My car is a {$cars[0]}";
?>
PHP 字符串函數(shù)
PHP strlen() 函數(shù)
strlen() 函數(shù)返回字符串的長度,以字符計。
strpos() 函數(shù)用于檢索字符串內(nèi)指定的字符或文本。
設(shè)置 PHP 常量
如需設(shè)置常量,請使用 define() 函數(shù) - 它使用三個參數(shù):
首個參數(shù)定義常量的名稱
第二個參數(shù)定義常量的值
可選的第三個參數(shù)規(guī)定常量名是否對大小寫敏感。默認(rèn)是 false。
<?php
define("GREETING", "Welcome to W3School.com.cn!");
echo GREETING;
?>
PHP foreach 循環(huán)
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
PHP 函數(shù)
- PHP 用戶定義函數(shù)
function functionName() {
被執(zhí)行的代碼;
}
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // 調(diào)用函數(shù)?>
- PHP 默認(rèn)參數(shù)值
1
<?php
function setHeight($minheight=50) {
echo "高度是:$minheight <br>";
}
setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>
- 如需使函數(shù)返回值,請使用 return 語句:
1
<?php
function sum($x,$y) {
$z=$x+$y;
return $z;
}
echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>
- PHP 關(guān)聯(lián)數(shù)組
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
<?php
$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");
foreach($age as $x=>$x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
- PHP 全局變量 - 超全局變量
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
- PHP 日期和時間
PHP Date() 函數(shù)
date(format,timestamp)
- 通過 PHP mktime() 創(chuàng)建日期
mktime(hour,minute,second,month,day,year)
- 通過 PHP strtotime() 用字符串來創(chuàng)建日期
strtotime(time,now)
trtotime("10:38pm April 15 2015");
<?php
$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br>";
?>