PHP學習筆記
阿新 • • 發佈:2020-11-18
PHP入門
php的四種不同的風格
<?php 程式碼 ?>
官方推薦風格
<? 程式碼 ?>
短標記風格,需配置檔案php.ini中開啟short+open_tag
<% 程式碼 %>
ASP風格,需配置檔案php.ini中開啟asp_tags
<script language=”php”> 程式碼 </script>
JavaScript標記的風格,這種風格在任何情況下都可以使用。
字串
單引號
變數和轉義序列都不會進行轉義
雙引號
PHP會處理雙引號括起來的字串、變數以及轉義序列。
<?php //demo $a = "雙引號"; echo "{$a}引起來的字串進行轉義\n"; $b = '單引號'; echo '$b引起來的字串不進行轉義\n'; echo "\n"; echo "$a,Hello"; ?>
這裡要注意字串裡面如果需要使用變數的話需要用{}
將變數括起來,或者在變數後面加一些符號,與後面的字元隔開。
字串切片
<?php
//demo
$s = 'PHP is the best language in the world!';
echo substr($s,0)."\n";
echo substr($s,0,6)."\n";
echo substr($s,-6,6)."\n";
echo substr($s,2,-6)."\n;";
?>
字串查詢
<?php //demo $s = 'PHP is the best language in the world! Php in my favorite! I just say PHP.'; echo "字串長度為:" . strlen($s) . "\n"; echo "PHP出現了" . substr_count($s,"PHP") . "次\n"; echo strstr($s,"Php") . "\n"; echo stristr($s, "Php") . "\n:"; //匹配時不區分大小寫 echo stripos($s , "PHP") . "\n"; //返回第一次匹配中的位置 ?>
字串替換
<?php
//demo
$s = 'PHP is the best language in the world!';
echo substr_replace($s, 'python', 0,3);
?>
正則
參考文件
陣列
組成
陣列由鍵值對組成,鍵可以是數字,也可以是字元。
<?php //demo $arr = array(2 => "a" , 3 => "c" , "this" => 'that'); echo '陣列元素個數:' .count($arr)."\n原陣列:"; //陣列鍵值對個數 print_r($arr); //列印陣列 echo "\nksort:"; ksort($arr); print_r($arr); //ksort按鍵從小到大排序 echo "\nasort:"; asort($arr); //asort按數值從小到大排序 print_r($arr); echo "\nsort:"; sort($arr); //sort按數值從小到大排序 print_r($arr); echo "\nrsort:"; rsort($arr); //rsort按索引從小到大排序 print_r($arr); echo "\n"; ?>
陣列操作符
- 使用
+
操作符號時,如果兩個陣列有相同的鍵,將不會新增。
<?php
//demo
$arr1 = array('1' => 1, '2' => 2, '3' => 3, '4' => 4 );
$arr2 = array('45' => 123, '6' => 8 );
$arr3 = array('1' => 4, '6' => 2);
var_dump($arr1 + $arr2);
echo "\n";
var_dump($arr1 + $arr3)
?>
訪問陣列
<?php
//demo
$r = range('a','z');
foreach ($r as $value) {
echo "$value";
}
echo "\n=====================\n";
foreach ($r as $key => $value) {
echo "$key=>$value ";
}
echo "\n$r[5]";
?>
陣列的建立與新增
<?php
//demo
$r = array('PHP' , "python" , "C" , "Java" , "C++" );
print_r($r);echo "\n";
$r[] = "Perl";
print_r($r);echo "\n";
$r[8] = "Ruby";
print_r($r);echo "\n";
$t = array('a' => "PHP" , "b" => "Python" , "c" => "C++" , "d" => "C++");
print_r($t);
?>
陣列元素的新增與刪除
<?php
$r = array("PHP","Python","C","Java","C++");
print_r($r);echo "\n";
array_unshift($r,"Ruby","Perl"); //在陣列頭部新增
print_r($r);echo "\n";
array_push($r,"Go"); //在陣列尾部新增
print_r($r);echo "\n";
array_shift($r); //刪除陣列第一個元素
print_r($r);echo "\n";
array_pop($r); //刪除陣列第一個元素
print_r($r);echo "\n";
?>
陣列與字串互轉
<?php
$r = array('PHP',"Python","Perl");
echo "初始化";print_r($r);echo "\n";
$r = implode('==',$r);
echo "After implode: ";print_r($r);echo "\n";
$r = explode("==",$r);
echo "After explode: ";print_r($r);echo "\n";
?>
in_array()判斷鍵是否在陣列中
<?php
$r = array("a" => 'PHP',"b" => "Python","c" => "Perl");
if (in_array('PHP', $r)) echo "PHP in array(r)";
else echo "PHP not in array(r)";
?>
array_merge()合併陣列
<?php
$r = array("a" => 'PHP',"b" => "Python","c" => "Perl");
$t = array("d" => "C" , "a" => "Go"); //這裡的a會將原來的a覆蓋掉
$n = array_merge($r,$t);
print_r($n);
?>
array_combine()
<?php
$r = array("a" => 'PHP',"b" => "Python","c" => "Perl");
$t = array("1" => "a" , "2" => "b" , '3' => 'c');
$n = array_combine($r,$t);
print_r($n);
?>
extrace()將陣列轉換成標量變數
<?php
$r = array("a" => 'PHP',"b" => "Python","c" => "Perl");
extract($r);
echo $a;echo "\n";
echo $b;echo "\n";
echo $c;echo "\n";
?>
array_keys()和array_values()
<?php
$r = array("a" => 'PHP',"b" => "Python","c" => "Perl");
print_r(arrat_keys($r));
echo "\n";
print_r(array_values($r));
echo "\n";
?>