PHP-标量类型

四种标量类型

  1. int (整型)
  2. float (浮点型,或 double )
  3. string (字符串)
  4. boolean (布尔型)

两种复合类型

  1. array (数组)
  2. object (对象)
  3. 两种特殊类型

resource (资源)

null (无类型)

var_dump()
查看值或类型。

gettype()
获取类型。

is_int()
是否为整型。

is_string()
是否为字符串类型。

round()
四舍五入

var_dump(round(25/7));//float(4)

floor()
返回不大于值得整数

echo floor((0.1+0.7)*10);//7

转义字符

  1. \ 反斜杠
  2. " 双引号

一个字符串可以用4种方式表达:单引号、双引号、 heredoc 、 nowdoc 。

heredoc 结构

<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* 含有变量的更复杂示例 */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.</br>
Now, I am printing some {$foo->bar[1]}.</br>
This should print a capital 'A': \x41
EOT;
?>
输出:
My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital 'A': A

nowdoc 结构

<?php
$str = <<<EOD
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
/* 含有变量的更复杂的示例 */
class foo
{
public $foo;
public $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.</br>
Now, I am printing some {$foo->bar[1]}.</br>
This should not print a capital 'A': \x41
EOT;
?>
输出:
My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital 'A': A

一个例子:

<?php
$juices = array("apple", "orange", "koolaid1" => "purple");
echo "He drank some $juices[0] juice."."</br>";
echo "He drank some $juices[1] juice."."</br>";
echo "He drank some juice made of $juices[0]."."</br>";
echo "He drank some $juices[koolaid1] juice."."</br>";
class people {
public $john = "John Smith";
public $jane = "Jane Smith";
public $robert = "Robert Paulsen";
public $smith = "Smith";
}
$people = new people();
echo "$people->john drank some $juices[0] juice."."</br>";
echo "$people->john then said hello to $people->jane."."</br>";
echo "$people->john's wife greeted $people->robert."."</br>";
echo "$people->robert greeted the two $people->smith.";
?>
输出:
He drank some apple juice.
He drank some orange juice.
He drank some juice made of apple.
He drank some purple juice.
John Smith drank some apple juice.
John Smith then said hello to Jane Smith.
John Smith's wife greeted Robert Paulsen.
Robert Paulsen greeted the two Smith.>
返回文章列表 文章二维码 打赏
本页链接的二维码
打赏二维码
期待你的评论

1 条评论
  1. summerlemon 大师   summerlemon  

    666