變量
- 為了實(shí)現(xiàn)程序邏輯和數(shù)據(jù)的復(fù)用在編程語(yǔ)言中引入了變量。
-
php變量以美元符號(hào)$后面跟隨數(shù)字,字母和下劃線組成的變量名構(gòu)成,變量名區(qū)分大小寫且以字母或者下劃線開(kāi)頭。 - 以下是一些變量的實(shí)例:
$title = 'hello, world!';
echo $title; // 輸出'hello, world!'
$number = 1;
echo $number; // 輸出1
$孫悟空 = '風(fēng)一般的男子'; // 漢字可以用作變量名
echo $孫悟空; // 輸出'風(fēng)一般的男子'
$_name = '孫悟空';
echo $_name; // 輸出'孫悟空'
$1name = 'tom'; // parse error, expecting `"variable (T_VARIABLE)"' or `'$''
-
=為賦值運(yùn)算符,表示將右邊的數(shù)據(jù)賦值給左邊的符號(hào)(如變量),一個(gè)變量可以賦值給另一個(gè)變量,如:
$title = 'hello, world!';
echo $title; // 輸出'hello, world!'
$subject = $title;
echo $subject; // 輸出'hello, world!'
- 可以聲明一個(gè)變量而不賦值,之后再對(duì)其進(jìn)行賦值,如:
$title;
echo $title; // 輸出''
$title = 'hello, world!';
echo $title; // 輸出'hello, world!'
-
php中存在可變變量這個(gè)概念,這樣可以動(dòng)態(tài)得設(shè)置和使用變量名,如:
$title = 'hello';
echo $title.'<br/>'; // 輸出'hello'
$$title = 'world';
echo $hello; // 輸出'world
-
.在php中可以用于字符拼接。 -
<br/>是html的一個(gè)標(biāo)簽,用于在網(wǎng)頁(yè)上進(jìn)行換行。
類型
- 在
php中數(shù)據(jù)總共有8中原始類型,分別為布爾型(boolean),整形(integer),浮點(diǎn)型(float),字符串(string),數(shù)組(array),對(duì)象(object),資源(resource)和無(wú)類型(NULL)。其中布爾型,整形,浮點(diǎn)型和字符串為四個(gè)標(biāo)量類型,數(shù)組和對(duì)象是兩個(gè)復(fù)合類型,資源和無(wú)類型是兩個(gè)特殊類型。 - 布爾型數(shù)據(jù)只有兩個(gè),
TRUE和FALSE,不區(qū)分大小寫,非真即假,常用做邏輯判斷。布爾數(shù)據(jù)的實(shí)例:
$data = TRUE;
echo $data.'<br/>'; // 輸出1
$data = true;
echo $data.'<br/>'; // 輸出1
$data = True;
echo $data.'<br/>'; // 輸出1
$data = FALSE;
echo $data.'<br/>'; // 輸出''
echo print_r($data).'<br/>'; // 輸出1
echo print_r($data, true).'<br/>'; // 輸出''
echo var_dump($data); // bool(false)
echo '<pre>';
echo print_r($data); // 輸出1
echo '</pre>';
echo '<pre>';
echo print_r($data, true); // 輸出''
echo '</pre>';
echo '<pre>';
echo var_dump($data); // 輸出bool(false)
echo '</pre>';
在
php中,print_r()和var_dump()都是可以用來(lái)打印變量的函數(shù),區(qū)別是var_dump()可以同時(shí)打印多個(gè)變量,并且可以輸出變量的類型,同時(shí)print_r()的返回值為布爾值,表示打印是否成功,而var_dump()的返回值就是打印輸出的變量本身。整型包含了所有整數(shù),整型數(shù)據(jù)可以用十進(jìn)制,二進(jìn)制(前置0b),八進(jìn)制(前置0)和十六進(jìn)制(前置0x)表示,每種表示都可以有正負(fù)之分。整型數(shù)據(jù)的實(shí)例:
$count = 10;
echo $count.'<br/>'; // 輸出10
$count = -10;
echo $count.'<br/>'; // 輸出-10
$count = 0b10;
echo $count.'<br/>'; // 輸出2
$count = -0b10;
echo $count.'<br/>'; // 輸出-2
$count = 010;
echo $count.'<br/>'; // 輸出8
$count = -010;
echo $count.'<br/>'; // 輸出-8
$count = 0x10;
echo $count.'<br/>'; // 輸出16
$count = -0x10;
echo $count.'<br/>'; // 輸出-16
$count = 0X10;
echo $count.'<br/>'; // 輸出16
$count = -0X10;
echo $count.'<br/>'; // 輸出-16
echo PHP_INT_MAX.'<br/>'; // 輸出9223372036854775807,PHP_INT_MAX是預(yù)定義常量,表示整型數(shù)最大值
echo (PHP_INT_MAX + 1).'<br/>'; // 輸出9.2233720368548E+18
- 浮點(diǎn)型就是浮點(diǎn)數(shù),也被稱作雙精度數(shù)(
double)或?qū)崝?shù)(real),就是帶有小數(shù)點(diǎn)的數(shù)字,有的浮點(diǎn)數(shù)在計(jì)算機(jī)中是沒(méi)有辦法精確表示的。浮點(diǎn)型數(shù)據(jù)的實(shí)例:
$number = 3.14;
echo $number.'<br/>'; // 輸出3.14
$number = 3.14e2; // 科學(xué)計(jì)數(shù)法
echo $number.'<br/>'; // 輸出314
$number = 3.14e-2; // 科學(xué)計(jì)數(shù)法
echo $number.'<br/>'; // 輸出0.0314
$number = 3.14E2; // 科學(xué)計(jì)數(shù)法
echo $number.'<br/>'; // 輸出314
$number = 3.14E-2; // 科學(xué)計(jì)數(shù)法
echo $number.'<br/>'; // 輸出0.0314
$number = 0.5;
echo $number.'<br/>'; // 輸出0.5
echo var_dump($number).'<br/>'; // 輸出float(0.5)
echo print_r($number).'<br/>'; // 輸出0.51
echo print_r($number, true).'<br/>'; // 輸出0.5
echo '<pre>';
echo var_dump($number); // 輸出float(0.5)
echo '</pre>';
echo '<pre>';
echo print_r($number); // 輸出0.51
echo '</pre>';
echo '<pre>';
echo print_r($number, true); // 輸出0.5
echo '</pre>';
/*
5 101
5 / 2 1
2 / 2 0
1 / 2 1
0
0.7 0.101100110011001100
0.7 * 2 1
0.4 * 2 0
0.8 * 2 1
0.6 * 2 1
0.2 * 2 0
0.4
通過(guò)二進(jìn)制轉(zhuǎn)換可以理解下面的輸出
*/
echo floor((0.1 + 0.7) * 10); // 輸出7
- 字符串由一系列字符組成,起始和結(jié)束位置分別有一個(gè)定界符,可能是
',",heredoc語(yǔ)法結(jié)構(gòu)或nowdoc語(yǔ)法結(jié)構(gòu)。字符串?dāng)?shù)據(jù)實(shí)例:
$title = 'this is a title';
echo $title.'<br/>'; // 輸出'this is a title'
$title = 'this is a title named "hello, world!"';
echo $title.'<br/>'; // 輸出'this is a title named "hello, world!"'
// $title = 'this is a title named 'hello, world!''; // localhost 網(wǎng)頁(yè)無(wú)法正常運(yùn)作
$title = 'this is a title named \'hello, world!\'';
echo $title.'<br/>'; // 輸出"this is a title named 'hello, world!'"
$title = 'this is a title named \hello, world!';
echo $title.'<br/>'; // 輸出'this is a title named \hello, world!'
$title = 'this is a title named \ hello, world!';
echo $title.'<br/>'; // 輸出'this is a title named \ hello, world!'
$title = 'this is a title named \\ hello, world!';
echo $title.'<br/>'; // 輸出'this is a title named \ hello, world!'
$title = "this is a title";
echo $title.'<br/>'; // 輸出'this is a title'
// $title = "this is a title named "hello, world!""; // localhost 網(wǎng)頁(yè)無(wú)法正常運(yùn)作
$title = "this is a title named 'hello, world!'";
echo $title.'<br/>'; // 輸出"this is a title named 'hello, world!'"
$title = "this is a title named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is a title named "hello, world!"'
$title = "this is a title \named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is a title amed "hello, world!"', 在mac os x下的結(jié)果,其它平臺(tái)可能不同
$title = "this is a \title named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is a itle named "hello, world!"', 在mac os x下的結(jié)果,其它平臺(tái)可能不同
$title = "this is a \r title named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is a title named "hello, world!"', 在mac os x下的結(jié)果,其它平臺(tái)可能不同
$title = "this is a \v title named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is a ? title named "hello, world!"', 在mac os x下的結(jié)果,其它平臺(tái)可能不同
$title = "this is \a title named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is \a title named "hello, world!"'
$title = "this is \\a title named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is \a title named "hello, world!"'
$title = "this is \ a title named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is \ a title named "hello, world!"'
$title = "this is \\ a title named \"hello, world!\"";
echo $title.'<br/>'; // 輸出'this is \ a title named "hello, world!"'
$title = 'this is a title \named "hello, world!"';
echo $title.'<br/>'; // 輸出'this is a title \named "hello, world!"'
$title = 'this is a \title named "hello, world!"';
echo $title.'<br/>'; // 輸出'this is a \title named "hello, world!"'
$title = 'this is a \r title named "hello, world!"';
echo $title.'<br/>'; // 輸出'this is a \r title named "hello, world!"'
$title = 'this is a \v title named "hello, world!"';
echo $title.'<br/>'; // 輸出'this is a \v title named "hello, world!"'
$title = 'this is a title named \"hello, world!\"';
echo $title.'<br/>'; // 輸出'this is a title named \"hello, world!\"'
$title = "this is a title named \'hello, world!\'";
echo $title.'<br/>'; // 輸出'this is a title named \'hello, world!\''
// 在雙引號(hào)包圍的字符串中,php會(huì)對(duì)變量和一些特殊字符(\n,\r,\t,\v,\\,\$等)進(jìn)行解析
$hello = 'hello, world!';
echo $hello.'<br/>'; // 輸出'hello, world!'
echo 'this is a title named $hello'.'<br/>'; // 輸出'this is a title named $hello'
echo "this is a title named $hello".'<br/>'; // 輸出'this is a title named hello, world!'
echo 'this is a title named \$hello'.'<br/>'; // 輸出'this is a title named \$hello'
echo "this is a title named \$hello".'<br/>'; // 輸出'this is a title named $hello'
/**
* heredoc結(jié)構(gòu)以<<<作為運(yùn)算符,后面接上標(biāo)識(shí)符,標(biāo)識(shí)符的命名規(guī)范同變量名一樣,換行后接字符串值,最后另起一行放置<<<后定義的標(biāo)識(shí)符作結(jié)尾,這一行除了標(biāo)識(shí)符和可能存在的分號(hào)外不能包含任何其它字符。
* heredoc結(jié)構(gòu)和雙引號(hào)一樣都可以對(duì)變量和特殊字符進(jìn)行解析。
*/
$title = <<<TITLE
this is a title
TITLE;
echo $title.'<br/>'; // 輸出'this is a title'
/*$title = <<<TITLE
this is a title
TITLE; */ // 網(wǎng)頁(yè)無(wú)法正常運(yùn)作
$title = <<<TITLE
this is a title
TITLE;
echo $title.'<br/>'; // 輸出'this is a title '
/*$title = <<<TITLE
this is a title
TITLE; */ // 網(wǎng)頁(yè)無(wú)法正常運(yùn)作
/*$title = <<<TITLE
this is a title
TITLE; // 網(wǎng)頁(yè)無(wú)法正常運(yùn)作
*/
/*$title = <<<TITLE
this is a title
TITLE; // 網(wǎng)頁(yè)無(wú)法正常運(yùn)作
*/
$title = <<<TITLE
"this is a title"
TITLE;
echo $title.'<br/>'; // 輸出'"this is a title"'
$title = <<<TITLE
'this is a title'
TITLE;
echo $title.'<br/>'; // 輸出"'this is a title'"
$title = <<<title
\'this is a title\'
title;
echo $title.'<br/>'; // 輸出"\'this is a title\'"
$title = <<<title
\"this is a title\"
title;
echo $title.'<br/>'; // 輸出'\"this is a title\"'
/*$title = <<<title
this is a title
TITLE;*/ // 網(wǎng)頁(yè)無(wú)法正常運(yùn)作
$title = <<<title
this is a title \named "hello, world!"
title;
echo $title.'<br/>'; // 輸出'this is a title amed "hello, world!"', 在mac os x下的結(jié)果,其它平臺(tái)可能不同
$title = <<<title
this is a \title named \"hello, world!\"
title;
echo $title.'<br/>'; // 輸出'this is a itle named \"hello, world!\"', 在mac os x下的結(jié)果,其它平臺(tái)可能不同
$title = <<<title
this is a \r title named \"hello, world!\"
title;
echo $title.'<br/>'; // 輸出'this is a title named \"hello, world!\"', 在mac os x下的結(jié)果,其它平臺(tái)可能不同
$title = <<<title
this is a \v title named \"hello, world!\"
title;
echo $title.'<br/>'; // 輸出'this is a ? title named \"hello, world!\"', 在mac os x下的結(jié)果,其它平臺(tái)可能不同
$title = <<<title
this is \a title named \"hello, world!\"
title;
echo $title.'<br/>'; // 輸出'this is \a title named \"hello, world!\"'
$title = <<<title
this is \\a title named \"hello, world!\"
title;
echo $title.'<br/>'; // 輸出'this is \a title named \"hello, world!\"'
$title = <<<title
this is \ a title named \"hello, world!\"
title;
echo $title.'<br/>'; // 輸出'this is \ a title named \"hello, world!\"'
$title = <<<title
this is \\ a title named \"hello, world!\"
title;
echo $title.'<br/>'; // 輸出'this is \ a title named \"hello, world!\"'
$title = <<<title
this is a title named \'hello, world!\'
title;
echo $title.'<br/>'; // 輸出"this is a title named \'hello, world!\'"
$title = <<<title
this is a title named $hello
title;
echo $title.'<br/>'; // 輸出"this is a title named hello, world!"
$title = <<<title
this is a title named \$hello
title;
echo $title.'<br/>'; // 輸出"this is a title named $hello"
$title = <<<"title"
this is a title named \$hello
title;
echo $title.'<br/>'; // 輸出"this is a title named $hello"
$title = <<<'title'
this is a title named \$hello
title;
echo $title.'<br/>'; // 輸出"this is a title named \$hello"
$title = <<<'title'
this is a title named $hello
title;
echo $title.'<br/>'; // 輸出"this is a title named $hello"
$title = <<<'title'
this is \a \\ \ \title \named $hello
title;
echo $title.'<br/>'; // 輸出"this is \a \\ \ \title \named $hello"
// nowdoc語(yǔ)法結(jié)構(gòu)和heredoc類似,區(qū)別是不會(huì)對(duì)特殊字符進(jìn)行解析,開(kāi)始處的標(biāo)識(shí)符用單引號(hào)引起來(lái)。
$doc = <<<'DOC'
this is a title,
this is a paragraph.
DOC;
echo $doc.'<br/>'; // 輸出'this is a title, this is a paragraph'
$doc = <<<'DOC'
this is a title,
this is a paragraph
DOC;
echo $doc.'<br/>';
$title = <<<'DOC'
this is a title
DOC;
echo $title.'<br/>'; // 輸出'this is a title'
$title = <<<'DOC'
this is a title named "hello, world!"
DOC;
echo $title.'<br/>'; // 輸出'this is a title named "hello, world!"'
$title = <<<'DOC'
this is a title named 'hello, world!'
DOC;
echo $title.'<br/>'; // 輸出"this is a title named 'hello, world!'"
$title = <<<'DOC'
this is a title named \'hello, world!\'
DOC;
echo $title.'<br/>'; // 輸出"this is a title named \'hello, world!\'"
$title = <<<'DOC'
this is a \r \v \title \named \\ \ \hello, world!
DOC;
echo $title.'<br/>'; // 輸出'this is a \r \v \title \named \\ \ \hello, world!'
$title = <<<'DOC'
this is a title named \"hello, world!\"
DOC;
echo $title.'<br/>'; // 輸出'this is a title named \"hello, world!\"'
$hello = 'hello, world!';
$title = <<<'DOC'
this is a title named $hello
DOC;
echo $title.'<br/>'; // 輸出'this is a title named $hello'
$title = <<<'DOC'
this is a title named \$hello
DOC;
echo $title.'<br/>'; // 輸出'this is a title named \$hello'
/*$title = <<<'DOC'
this is a title named \$hello
doc;*/ // 網(wǎng)頁(yè)無(wú)法正常運(yùn)作
本文首發(fā)于公眾號(hào):programmer_cc,轉(zhuǎn)載請(qǐng)注明出處。

微信公眾號(hào).jpg