|
<!DOCTYPE html>
<html>
<title>基本类型</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<body>
<a href="../index.php">返回首页</a>
<hr></hr>
<?php
#定义boolean变量
$a_bool = false; // a boolean
#转换示例
$foo = "0"; // $foo 是字符串 (ASCII 48)
$foo += 2; // $foo 现在是一个整数 (2)
$foo = $foo + 1.3; // $foo 现在是一个浮点数 (3.3)
$foo = 5 + "10 Little Piggies"; // $foo 是整数 (15)
$foo = 5 + "10 Small Pigs"; // $foo 是整数 (15)
?>
<p>1、布尔类型Boolean:</p>
值可以为true或false。要明示地将一个值转换成boolean,用(bool)或者(boolean)来强制转换。
<? #强转为Boolean型
echo gettype($a_bool);
echo "<br/>";var_dump($a_bool) ;
echo "<br/>";var_dump((bool) ""); // bool(false)
echo "<br/>";var_dump((bool) 1); // bool(true)
echo "<br/>";var_dump((bool) -2); // bool(true)
echo "<br/>";var_dump((bool) "foo"); // bool(true)
echo "<br/>";var_dump((bool) 2.3e5); // bool(true)
echo "<br/>";var_dump((bool) array(12)); // bool(true)
echo "<br/>";var_dump((bool) array()); // bool(false)
echo "<br/>";var_dump((bool) "false"); // bool(true)
?><br/>
<hr></hr>
<p>2、整型integer:</p>
<?php #定义integer变量
$a1 = 1234; // 十进制数
$a2 = -123; // 一个负数
$a3 = 0123; // 八进制数(等于十进制的 83)
$a4 = 0x1A; // 十六进制数(等于十进制的 26)
?>
integer 超出范围,将被解释为 float。运算结果超出了 integer 范围,也会返回 float。<br/>
<? #整型
echo gettype($a1);
echo "<br/>";var_dump($a1) ;
echo "<br/>";var_dump($a2) ;
echo "<br/>";var_dump($a3) ;
echo "<br/>";var_dump($a4) ;
$large_number = 2147483647;
echo "<br/>";var_dump($large_number);// 输出为:int(2147483647)
$large_number = 2147483648;
echo "<br/>";var_dump($large_number);// 输出为:float(2147483648)
// 同样也适用于十六进制表示的整数: 从 2^31 到 2^32-1:
echo "<br/>";var_dump( 0xffffffff );// 输出: float(4294967295)
// 不适用于大于 2^32-1 的十六进制表示的数:
echo "<br/>";var_dump( 0x100000000 );// 输出: int(2147483647)
$million = 1000000;
$large_number = 50000 * $million;
echo "<br/>";var_dump($large_number);// 输出: float(50000000000)
?>
<br/>PHP 中没有整除的运算符。1/2 产生出 float 0.5。可以总是舍弃小数部分,或使用 round() 函数。
<br/>从布尔值转换FALSE为0,TRUE 将为1。
<?php #值的强转
echo "<br/>";var_dump(25/7); // float(3.5714285714286)
echo "<br/>";var_dump((int) (25/7)); // int(3)
echo "<br/>";var_dump((integer) (25/7)); // int(3)
echo "<br/>";var_dump(intval (25/7)); // int(3)
echo "<br/>";var_dump(round(25/7)); // float(4)
?><br/>
<hr></hr>
<p>3、浮点型</p>
浮点数(也叫浮点数,双精度数或实数)可以用以下任一语法定义:<br/>
永远不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相等。如果确实需要更高的精度,应该使用任意精度数学函数或者 gmp 函数。<br>
<?php #浮点型
$af = 1.234;
$bf = 1.2e3;
$cf = 7E-10;
echo "<br/>";var_dump($af);
echo "<br/>";var_dump($bf);
echo "<br/>";var_dump($cf);
?><br/>
<hr></hr>
<p>4、Strings</p>
字符串可以用单引号、双引号、定界符三种字面上的方法定义。<br/>
如果用双引号(””)括起字符串,PHP 懂得更多特殊字符的转义序列。\n 换行 \r 回车 \t 水平制表符 \\ 反斜线 \$ 美圆符号 \” 双引号<br/>
\[0-7]{1,3} 此正则表达式序列匹配一个用八进制符号表示的字符<br/>
\x[0-9A-Fa-f]{1,2} 此正则表达式序列匹配一个用十六进制符号表示的字符<br/>
<?php
echo "<br/>";echo 'this is a simple string';
echo "<br/>";echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
echo "<br/>";echo 'Arnold once said: "I\'ll be back"';// Outputs: Arnold once said: "I'll be back"
echo "<br/>";echo 'You deleted C:\\*.*?';// Outputs: You deleted C:\*.*?
echo "<br/>";echo "\n 换行 \r 回车 \t 水平制表符 \\ 反斜线 \$ 美圆符号 \" 双引号";
echo "<br/>";echo 'You deleted C:\*.*?';// Outputs: You deleted C:\*.*?
echo "<br/>";echo 'This will not expand: \n a newline';// Outputs: This will not expand: \n a newline
echo "<br/>"; echo 'Variables do not $expand $either';// Outputs: Variables do not $expand $either
?><br/>
<b>定界符</b>:
另一种给字符串定界的方法使用定界符语法(”<<<”)。
应该在<<<之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串。<br/>
用echo来输出大段的html和javascript脚本<br/>
1.PHP定界符的作用就是按照原样,包括换行格式什么的,输出在其内部的东西;<br/>
2.在PHP定界符中的任何特殊字符都不需要转义;<br/>
3.PHP定界符中的PHP变量会被正常的用其值来替换。<br/>
<?
$name = "123";
print<<<XX
hello{$name}
XX;
?>
<br/>
1.在<<<之后的字符Eof是自己定义的,随便什么都是可以的,但是结尾处的字符一定要和他一样,他们是成对出现的,就像{}这样的——这是最基本的;<br/>
2.结尾的一行(如上例的Eof;),一定要另起一行,并且改行除了Eof;这个定界符结尾标识之外不能有任何其他字符,前后都不能有,包括空格。如果在本行最前或者最后出现空格,制表符的话,你会收到一个这样的错误信息:Parse error: parse error, unexpected $end in……,提示你语法错误;<br/>
3.如果在定界符中间出现有PHP的变量,你只需要像在其它字符串中输出一样写就行了.变量$name之所以要用{}括起来是要告诉PHP解析器这是一个PHP变量,其实不用也是可以的,但是有可能会产生歧义<br/>
<hr></hr>
<p>5、数组</p>
PHP 中的 数组 实际上是一个有序映射。映射是一种把 values 关联到 keys 的类型。此类型在很多方面做了优化,因此可以把它当成真正的数组,或列表(向量),散列表(是映射的一种实现),字典,集合,栈,队列以及更多可能性。数组元素的值也可以是另一个数组。树形结构和多维数组也是允许的。<br/>
<?php #数组
//可以用 array() 语言结构来新建一个 array。它接受任意数量用逗号分隔的 键(key) => 值(value) 对。
$arr = array("foo" => "bar", 12 => true);
echo "<br/>";echo $arr["foo"]; // bar
echo "<br/>";echo $arr[12]; // 1
?> <br/>
key 可以是 integer 或者 string。如果key是一个 integer 的标准表示,则被解释为整数(例如 "8" 将被解释为 8,而 "08" 将被解释为 "08")。key 中的浮点数被取整为 integer。在 PHP 中索引数组与关联 数组 是相同的,它们都可以同时包含 整型 和 字符串 的下标。值可以是任意的 PHP 类型。<br/>
<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo "<br/>";echo $arr["somearray"][6]; // 5
echo "<br/>";echo $arr["somearray"][13]; // 9
echo "<br/>";echo $arr["somearray"]["a"]; // 42
?> <br/>
用方括号的语法新建/修改 $arr[key] = value;$arr[] = value;/key 可以是 integer 或 string; value 可以是任意类型的值<br/>
如果 $arr 还不存在,将会新建一个。这也是一种定义数组的替换方法。要改变一个值,只要给它赋一个新值。如果要删除一个键名/值对,要对它用 unset()。 <br/>
<?php
$arr = array(5 => 1, 12 => 2);
$arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script
$arr["x"] = 42; // This adds a new element to
// the array with key "x"
unset($arr[5]); // This removes the element from the array
unset($arr); // This deletes the whole array
echo "<br/>";
print_r($arr);
// 创建一个简单的数组
echo "<br/>创建数组";
$array = array(1, 2, 3, 4, 5);
print_r($array);
// 现在删除其中的所有元素,但保持数组本身不变:
foreach ($array as $i => $value) {
unset($array[$i]);
}
echo "<br/>删除元素";
print_r($array);
// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)
$array[] = 6;
echo "<br/>添加一个单元";
print_r($array);
// 重新索引:
echo "<br/>重新索引";
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
unset() 函数允许删除数组中的某个键。但要注意数组将不会重建索引。<br/>
array_values();重新建立索引<br/>
foreach 控制结构是专门用于数组的。它提供了一个简单的方法来遍历数组。<br/>
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// Simple array:
$array = array(1, 2);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
echo "<br/>";echo "\nChecking $i: \n";
echo "<br/>";echo "Bad: " . $array['$i'] . "\n";
echo "<br/>";echo "Good: " . $array[$i] . "\n";
echo "<br/>";echo "Bad: {$array['$i']}\n";
echo "<br/>";echo "Good: {$array[$i]}\n";
}
?>
<br/>
<b>1 使用 array()</b><br/>
<?php #1 使用 array()
// This:
$a = array( 'color' => 'red',
'taste' => 'sweet',
'shape' => 'round',
'name' => 'apple',
4 // key d be 0
);
echo "<br/>";print_r($a);
$b = array('a', 'b', 'c');
echo "<br/>";print_r($b);
// . . .is completely equivalent with this:
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // key will be 0
echo "<br/>";print_r($a);
$b = array();
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';
echo "<br/>";print_r($b);
?><br/>
<b>2 集合</b><br/>
<?php #2 集合
// Array as (property-)map
$map = array( 'version' => 4,
'OS' => 'Linux',
'lang' => 'english',
'short_tags' => true
);
echo "<br/>";print_r($map);
// strictly numerical keys
$array = array( 7,
8,
0,
156,
-10
);// this is the same as array(0 => 7, 1 => 8, ...)
echo "<br/>";print_r($array);
$switching = array( 10, // key = 0
5 => 6,
3 => 7,
'a' => 4,
11, // key = 6 (maximum of integer-indices was 5)
'8' => 2, // key = 8 (integer!)
'02' => 77, // key = '02'
0 => 12 // the value 10 will be overwritten by 12
);
echo "<br/>";print_r($switching);
// empty array
$empty = array();
echo "<br/>";print_r($empty);
?><br/>
<b>3 集合</b><br/>
<?php #3 集合
// PHP 5
$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 */
echo "<br/>";
// Workaround for older versions
foreach ($colors as $key => $color) {
$colors[$key] = strtoupper($color);
}
echo "<br/>";print_r($colors);
?> <br/>
<b>4 下标从1开始的数组</b><br/>
<?php #4 下标从1开始的数组
$firstquarter = array(1 => 'January', 'February', 'March');
echo "<br/>";print_r($firstquarter);
?> <br/>
<b>5 填充数组</b><br/>
<?php #5 填充数组
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?> <br/>
<b>6 数组排序</b><br/>
<?php #6 数组排序
sort($files);
print_r($files);
?> <br/>
<b>7 递归和多维数组</b><br/>
<?php #7 递归和多维数组
$fruits = array ( "fruits" => array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third"
)
);
// Some examples to address values in the array above
echo "<br/>";echo $fruits["holes"][5]; // prints "second"
echo "<br/>";echo $fruits["fruits"]["a"]; // prints "orange"
unset($fruits["holes"][0]); // remove "first"
// Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
?> <br/>
<b>8 值的拷贝 使用&引用操作符 通过引用来拷贝数组</b><br/>
<?php #8 值的拷贝
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
// $arr1 is still array(2, 3)
$arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
echo "<br/>";print_r($arr2); //
echo "<br/>";print_r($arr3); //
?>
<br/>
<hr></hr>
<p>6、对象</p>
对象初始化 要创建一个新的对象 object, 使用 new 语句实例化一个类:
<?php #对象初始化
class foo
{
function do_foo()
{
echo "<br/>";echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo();
?> <br/>
<?php #转换为对象
$obj = (object) 'ciao';
echo "<br/>";echo $obj->scalar; // outputs 'ciao'
?>
<hr></hr>
<p>7、NULL</p>
特殊的 NULL 值表示一个变量没有值。NULL 类型唯一可能的值就是 NULL。 <br/>
被赋值为 NULL。 尚未被赋值。 被 unset()。 <br/>
<?php
$var = NULL;
echo "<br/>"; #echo $var; // outputs 'ciao'
var_dump($var)
?>
</body>
</html>
|
|