PHP manual(update)

2017-02-15  本文已影响1228人  仇诺伊
<?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";
}
?>
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
)

<?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
)

objects

上一篇下一篇

猜你喜欢

热点阅读