php初级讲义4-变量和类型

变量

  • 为了实现程序逻辑和数据的复用在编程语言中引入了变量。
  • php变量以美元符号$后面跟随数字,字母和下划线组成的变量名构成,变量名区分大小写且以字母或者下划线开头。
  • 以下是一些变量的实例:
$title = 'hello, world!';
echo $title; // 输出'hello, world!'
$number = 1;
echo $number; // 输出1
$孙悟空 = '风一般的男子'; // 汉字可以用作变量名
echo $孙悟空; // 输出'风一般的男子'
$_name = '孙悟空';
echo $_name; // 输出'孙悟空'
$1name = 'tom'; // parse error, expecting `"variable (T_VARIABLE)"' or `'$''
  • =为赋值运算符,表示将右边的数据赋值给左边的符号(如变量),一个变量可以赋值给另一个变量,如:
$title = 'hello, world!';
echo $title; // 输出'hello, world!'
$subject = $title;
echo $subject; // 输出'hello, world!'
  • 可以声明一个变量而不赋值,之后再对其进行赋值,如:
$title;
echo $title; // 输出''
$title = 'hello, world!';
echo $title; // 输出'hello, world!'
  • php中存在可变变量这个概念,这样可以动态得设置和使用变量名,如:
$title = 'hello';
echo $title.'<br/>'; // 输出'hello'
$$title = 'world';
echo $hello;  // 输出'world
  • .php中可以用于字符拼接。
  • <br/>html的一个标签,用于在网页上进行换行。

类型

  • php中数据总共有8中原始类型,分别为布尔型(boolean),整形(integer),浮点型(float),字符串(string),数组(array),对象(object),资源(resource)和无类型(NULL)。其中布尔型,整形,浮点型和字符串为四个标量类型,数组和对象是两个复合类型,资源和无类型是两个特殊类型。
  • 布尔型数据只有两个,TRUEFALSE,不区分大小写,非真即假,常用做逻辑判断。布尔数据的实例:
$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()都是可以用来打印变量的函数,区别是var_dump()可以同时打印多个变量,并且可以输出变量的类型,同时print_r()的返回值为布尔值,表示打印是否成功,而var_dump()的返回值就是打印输出的变量本身。

  • 整型包含了所有整数,整型数据可以用十进制,二进制(前置0b),八进制(前置0)和十六进制(前置0x)表示,每种表示都可以有正负之分。整型数据的实例:

$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是预定义常量,表示整型数最大值
echo (PHP_INT_MAX + 1).'<br/>'; // 输出9.2233720368548E+18
  • 浮点型就是浮点数,也被称作双精度数(double)或实数(real),就是带有小数点的数字,有的浮点数在计算机中是没有办法精确表示的。浮点型数据的实例:
$number = 3.14;
echo $number.'<br/>'; // 输出3.14
$number = 3.14e2; // 科学计数法
echo $number.'<br/>'; // 输出314
$number = 3.14e-2; // 科学计数法
echo $number.'<br/>'; // 输出0.0314
$number = 3.14E2; // 科学计数法
echo $number.'<br/>'; // 输出314
$number = 3.14E-2; // 科学计数法
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
通过二进制转换可以理解下面的输出
*/
echo floor((0.1 + 0.7) * 10); // 输出7
  • 字符串由一系列字符组成,起始和结束位置分别有一个定界符,可能是', ", heredoc语法结构nowdoc语法结构。字符串数据实例:
$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 网页无法正常运作
$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 网页无法正常运作
$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下的结果,其它平台可能不同
$title = "this is a \title named \"hello, world!\"";
echo $title.'<br/>'; // 输出'this is a itle named "hello, world!"', 在mac os x下的结果,其它平台可能不同
$title = "this is a \r title named \"hello, world!\"";
echo $title.'<br/>'; // 输出'this is a title named "hello, world!"', 在mac os x下的结果,其它平台可能不同
$title = "this is a \v title named \"hello, world!\"";
echo $title.'<br/>'; // 输出'this is a � title named "hello, world!"', 在mac os x下的结果,其它平台可能不同
$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!\''
// 在双引号包围的字符串中,php会对变量和一些特殊字符(\n,\r,\t,\v,\\,\$等)进行解析
$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结构以<<<作为运算符,后面接上标识符,标识符的命名规范同变量名一样,换行后接字符串值,最后另起一行放置<<<后定义的标识符作结尾,这一行除了标识符和可能存在的分号外不能包含任何其它字符。
 * heredoc结构和双引号一样都可以对变量和特殊字符进行解析。
 */
$title = <<<TITLE
    this is a title
TITLE;
echo $title.'<br/>'; // 输出'this is a title'
/*$title = <<<TITLE
    this is a title
TITLE;  */ // 网页无法正常运作
$title = <<<TITLE
    this is a title

TITLE;
echo $title.'<br/>'; // 输出'this is a title '
/*$title = <<<TITLE
    this is a title
TITLE; */ // 网页无法正常运作
/*$title = <<<TITLE
    this is a title
TITLE; // 网页无法正常运作
*/
/*$title = <<<TITLE
    this is a title
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;
echo $title.'<br/>'; // 输出'\"this is a title\"'
/*$title = <<<title
    this is a title
TITLE;*/ // 网页无法正常运作
$title = <<<title
    this is a title \named "hello, world!"
title;
echo $title.'<br/>'; // 输出'this is a title amed "hello, world!"', 在mac os x下的结果,其它平台可能不同
$title = <<<title
    this is a \title named \"hello, world!\"
title;
echo $title.'<br/>'; // 输出'this is a itle named \"hello, world!\"', 在mac os x下的结果,其它平台可能不同
$title = <<<title
    this is a \r title named \"hello, world!\"
title;
echo $title.'<br/>'; // 输出'this is a title named \"hello, world!\"', 在mac os x下的结果,其它平台可能不同
$title = <<<title
    this is a \v title named \"hello, world!\"
title;
echo $title.'<br/>'; // 输出'this is a � title named \"hello, world!\"', 在mac os x下的结果,其它平台可能不同
$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语法结构和heredoc类似,区别是不会对特殊字符进行解析,开始处的标识符用单引号引起来。
$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;*/ // 网页无法正常运作

本文首发于公众号:programmer_cc,转载请注明出处。


微信公众号.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容