php初級(jí)講義4-變量和類型

變量

  • 為了實(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è),TRUEFALSE,不區(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,317評(píng)論 0 17
  • //Clojure入門教程: Clojure – Functional Programming for the J...
    葡萄喃喃囈語(yǔ)閱讀 4,036評(píng)論 0 7
  • 個(gè)人學(xué)習(xí)批處理的初衷來(lái)源于實(shí)際工作;在某個(gè)迭代版本有個(gè)BS(安卓手游模擬器)大需求,從而在測(cè)試過(guò)程中就重復(fù)涉及到...
    Luckykailiu閱讀 4,981評(píng)論 0 11
  • 轉(zhuǎn)載 原文的排版和內(nèi)容都更加友好,并且詳細(xì),我只是在這里貼出了一部分留作自己以后參考和學(xué)習(xí),如希望更詳細(xì)了解AWK...
    XKirk閱讀 3,364評(píng)論 2 25
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139

友情鏈接更多精彩內(nèi)容