陣列:
<?php
$array = array(
“foo” => “bar”,
“bar” => “foo”,
);
陣列列印:
$a = array(
“one” => 1,
“two” => 2,
“three” => 3,
“seventeen” => 17
);
foreach ($a as $k => $v) {
echo “\$a[$k] => $v.\n”;
}
網頁內容:
<?php
$homepage = file_get_contents(‘http://www.example.com/’);
echo $homepage;
?>
分割字串:
$pizza = “piece1 piece2 piece3 piece4 piece5 piece6”;
$pieces = explode(” “, $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
解析 JSON 字串:
<?php
$json = ‘{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}’;
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
取代字串:
// Provides: <body text=’black’>
$bodytag = str_replace(“%body%”, “black”, “<body text=’%body%’>”);
沒有留言:
張貼留言