<? 和 ?>
,但不鼓励使用。只有通过激活 php.ini 中的 short_open_tag 配置指令或者在编译 PHP 时使用了配置选项 --enable-short-tags 时才能使用短标记。<?php ?>
和 <script language="php"> </script>
总是可用的。另两种是短标记和 ASP 风格标记,可以在 php.ini 配置文件中打开或关闭。尽管有些人觉得短标记和 ASP 风格标记很方便,但移植性较差,通常不推荐使用。
Note:
此外注意如果将 PHP 嵌入到 XML 或 XHTML 中则需要使用 <?php ?>
标记以保持符合标准。var_dump()
函数。
如果只是想得到一个易读懂的类型的表达方式用于调试,用 gettype()
函数。要查看某个类型,不要用gettype()
,而用 is_type
函数。以下是一些范例:<?php
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer
echo gettype($a_bool); // prints out: boolean
echo gettype($a_str); // prints out: string
// If this is an integer, increment it by four
if (is_int($an_int)) {
$an_int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?>
settype()
函数。var_dump((bool) ""); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -1); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
结果
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
$a = 1234; // 十进制数
$a = -123; // 负数
$a = 0123; // 八进制数 (等于十进制 83)
$a = 0x1A; // 十六进制数 (等于十进制 26)
$large_number = 2147483647;
var_dump($large_number); // int(2147483647)
$large_number = 2147483648;
var_dump($large_number); // float(2147483648)
$million = 1000000;
$large_number = 50000 * $million;
var_dump($large_number); // float(50000000000)
int(2147483647)
double(2147483648)
double(50000000000)
64位的溢出
$large_number = 9223372036854775807;
var_dump($large_number); // int(9223372036854775807)
$large_number = 9223372036854775808;
var_dump($large_number); // float(9.2233720368548E+18)
$million = 1000000;
$large_number = 50000000000000 * $million;
var_dump($large_number); // float(5.0E+19)
double(9.2233720368548E+18)
double(9.2233720368548E+18)
double(5.0E+19)
var_dump(25/7); // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7)); // double(4)
<?php
/**
* Created by Zoe.
* User: Administrator
* Date: 2017/2/8
* Time: 9:16
*/
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* 含有变量的更复杂示例 */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foods';
$this->bar = array('apple', 'strawberry', 'watermelon');
}
}
$foo = new foo();
$name = 'Zoe';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
My name is "Zoe". I am printing some Foods.
Now, I am printing some strawberry.
This should print a capital 'A': A
Process finished with exit code 0
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// 自 PHP 5.4 起
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
print_r($array);
// 现在删除其中的所有元素,但保持数组本身不变:
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);
// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)
$array[] = 6;
print_r($array);
// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
)
Array
(
[5] => 6
)
Array
(
[0] => 6
[1] => 7
)
"$foo[bar]" 是合法的(“合法”的原文为 valid。在实际测试中,这么做确实可以访问数组的该元素,但是会报一个常量未定义的 notice。无论如何,强烈建议不要使用 $foo[bar]
这样的写法,而要使用 $foo['bar'] 来访问数组中元素。--haohappy 注)。至于为什么参见以上的例子和字符串中的变量解析中的解释.<?php
/**
* Created by Zoe.
* User: Administrator
* Date: 2017/2/15
* Time: 10:24
*/
$a = array(
'color' => 'red',
'taste' => 'sweet',
'shape' => 'round',
'name' => 'apple',
4
);
print_r($a);
$b = array('a','b','c');
print_r($b);
echo "另一种数组形式,结果相同";
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a['0'] = '4';
print_r($a);
$b = array();
$b['0'] = 'a';
$b['1'] = 'b';
$b['2'] = 'c';
print_r($b);
输出结果
Array
(
[color] => red
[taste] => sweet
[shape] => round
[name] => apple
[0] => 4
)
Array
(
[0] => a
[1] => b
[2] => c
)
另一种数组形式,结果相同Array
(
[color] => red
[taste] => sweet
[shape] => round
[name] => apple
[0] => 4
)
Array
(
[0] => a
[1] => b
[2] => c
)
<?php
/**
* Created by Zoe.
* User: Administrator
* Date: 2017/2/15
* Time: 10:24
*/
$color = array('red','blue','yellow','black');
foreach ($color as $value) {
echo "do you like $value?\n";
}
结果
do you like red?
do you like blue?
do you like yellow?
do you like black?
直接改变数组的值自 PHP 5 起可以通过引用传递来做到。之前的版本需要需要采取变通的方法
<?php
/**
* Created by Zoe.
* User: Administrator
* Date: 2017/2/15
* Time: 10:24
*/
echo '在循环中改变单元';
$colors = array('red', 'blue', 'green', 'yellow');
foreach ($colors as &$color) {
$color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */
// Workaround for older versions
foreach ($colors as $key => $color) {
$colors[$key] = strtoupper($color);
}
print_r($colors);
结果
在循环中改变单元Array
(
[0] => RED
[1] => BLUE
[2] => GREEN
[3] => YELLOW
)
<?php
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?>
可以用 count() 函数来数出数组中元素的个数。 可以用sort()来对数组进行排序.
<?php
/**
* Created by Zoe.
* User: Administrator
* Date: 2017/2/15
* Time: 10:24
*/
//echo '递归和多维数组';
$fruits = array(
'fruits' => array(
'a' => 'orange',
'b' => 'apple',
'c' => 'pear'
),
'numbers' => array(
'1','2','3','4','5','6'
),
'holes' => array(
'first',
'5' => 'second',
'three'
)
);
//unset($fruits['holes']['0']);
$juices["apple"]["green"] = "good";
print_r($fruits);
print_r($juices);
运行结果
Array
(
[fruits] => Array
(
[a] => orange
[b] => apple
[c] => pear
)
[numbers] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
[holes] => Array
(
[0] => first
[5] => second
[6] => three
)
)
Array
(
[apple] => Array
(
[green] => good
)
)
<?php
/**
* Created by Zoe.
* User: Administrator
* Date: 2017/2/15
* Time: 10:24
*/
//echo '数组(Array) 的赋值总是会涉及到值的拷贝。使用引用运算符通过引用来拷贝数组。';
$arr1 = array(2,3);
$arr2 = $arr1;
$arr2[] = 4;
$arr3 = &$arr1;
//print_r($arr3);exit();
$arr3[] = 4;
print_r($arr1);
print_r($arr2);
print_r($arr3);
运行结果
Array
(
[0] => 2
[1] => 3
[2] => 4
)
Array
(
[0] => 2
[1] => 3
[2] => 4
)
Array
(
[0] => 2
[1] => 3
[2] => 4
)