美文网首页PHP DevPHPPHP学习
php初级讲义4-变量和类型

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

作者: Stone_Zhuo | 来源:发表于2017-02-17 22:06 被阅读60次

变量

  • 为了实现程序逻辑和数据的复用在编程语言中引入了变量。
  • 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

相关文章

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

    变量 为了实现程序逻辑和数据的复用在编程语言中引入了变量。 php变量以美元符号$后面跟随数字,字母和下划线组成的...

  • php变量和数据类型

    php中的变量 PHP中的变量声明 PHP中的变量的使用 PHP中的数据类型之整型 PHP数据类型之浮点类型和布尔...

  • php中的变量和常量

    php是弱类型语言,变量类型和存入的值相关 变量 php中声明一个变量用 $符 php中变量的命名规范 可变变量:...

  • stata文本变量的处理

    资料来源 :连玉君Stata 初级 研讨班讲义 以文字类型存储的数字之转换 -destring- 纯文字类别变量之...

  • PHP学习线路图

    PHP教程 PHP教程PHP简介PHP环境设置PHP语法概述PHP变量类型PHP常量类型 PHP运算符类型PHP ...

  • 2019-03-11 PHP学习笔记之PHP的基本语法(2)

    参考资料:《细说PHP》、《PHP、MySQL与JavaScript学习手册》 变量的类型 变量类型指保存在该变量...

  • php开发之变量的类型判断

    php变量的类型判断: 1.var_dump() 描述:打印变量的相关信息(包括变量的类型和值) 语法:void ...

  • PHP 实例

    PHP 实例 运行PHP脚本PHP Double 数据类型PHP 字符串变量PHP 文档PHP局部变量PHP函数参...

  • php基础学习变量篇

    1.php为弱语言类型(变量没有类型),严格区分大小写 2.php变量 PHP 有四种不同的变量作用域local ...

  • PHP学习线路图 PHP教程

    PHP学习线路图 PHP教程 PHP教程PHP简介PHP环境设置PHP语法概述PHP变量类型PHP常量类型 PHP...

网友评论

    本文标题:php初级讲义4-变量和类型

    本文链接:https://www.haomeiwen.com/subject/goeuittx.html