PHP常用內置函數

  1. simplexml_load_file();
    simplexml_load_file() 函數把 XML 文檔載入對象中。
    輸出:

    object(SimpleXMLElement)#2 (1) {
      ["resolvedomains"]=>
      string(5) "false"
    }
    
  2. gethostbyaddr(127.0.0.1);
    返回對應於給定地址的主機信息。

  3. http_build_query()
    http_build_query — 生成 URL-encode 之後的請求字符串

    <?php
    $data = [
    	'name' => "lifangpign",
    	'age' => 25,
    	'sex' => "男",
    	'phone' =>18403433575
    ];
    
    $string = http_build_query($data);
    var_dump($string);
    

    結果:

    string(54) "name=lifangpign&age=25&sex=%E7%94%B7&phone=18403433575"
    
  4. substr_count()
    計算子串在字符串中出現的次數。

    echo substr_count("I love Shanghai. Shanghai is the biggest city in china.","Shanghai");
    

    結果:2

  5. array_reverse:返回翻轉順序的數組。

    //例子
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
    print_r(array_reverse($a));
    //輸出:
    Array ( [c] => Horse [b] => Cat [a] => Dog )
    
  6. array_merge:函數把兩個或多個數組合併爲一個數組。

    // 例子 1
    $a1=array("a"=>"Horse","b"=>"Dog");
    $a2=array("c"=>"Cow","b"=>"Cat");
    print_r(array_merge($a1,$a2));
    // 輸出:後邊的鍵值覆蓋前面的鍵值
    Array ( [a] => Horse [b] => Cat [c] => Cow )
    
  7. substr(string,start,length):字符串截取

    // 例子
    echo substr("hello word", 6, 4);
    // 輸出
    word
    
  8. in_array():搜索數組中是否存在指定的值

    // 例子
    var_dump(in_array(1, [2, 6, 9, 1]));
    // 輸出
    bool(true)
    
  9. basename():返回路徑中的文件名部分。

    // 例子
    $file = "/testweb/home.php";
    echo basename($file);
    // 輸出
    "home.php"
    
  10. gettype():返回變量的類型
    結果可能是:boolean、integer、double、string、array、object、resource等

  11. method_exists ( mixed $object , string $method_name ):檢查類的方法是否存在

    class Test(){
    	public function hello(){
    		echo "hello world";
    	}
    }
    $test = new Test();
    var_dump(method($test, "hello"));
    // 結果
    bool(true)
    

未完待續…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章