PHP DevPHPPHP学习

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

2017-02-17  本文已影响60人  Stone_Zhuo

变量

$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!'
$title = 'hello';
echo $title.'<br/>'; // 输出'hello'
$$title = 'world';
echo $hello;  // 输出'world

类型

$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>';
$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
$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
$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
上一篇 下一篇

猜你喜欢

热点阅读