PHP常用函數

來源:互聯網
上載者:User
下面是我整理的PHP中常用的函數,有興趣的小夥伴可以去看看。

array_intersect()

比較兩個數組的索引值,並返回交集:

<?php$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");$a2=array("e"=>"red","f"=>"green","g"=>"blue");$result=array_intersect($a1,$a2);print_r($result);?>result:Array ( [a] => red [b] => green [c] => blue )

array_keys() 函數

返回包含數組中所有鍵名的一個新數組。

<?php$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");print_r(array_keys($a));?>result:Array ( [0] => Volvo [1] => BMW [2] => Toyota )123456

array_key_exists() 函數

檢查某個數組中是否存在指定的鍵名,如果鍵名存在則返回 true,如果鍵名不存在則返回 false。

<?php    $a=array("Volvo"=>"XC90","BMW"=>"X5");    if (array_key_exists("Volvo",$a))      {      echo "鍵存在!";      }    else      {      echo "鍵不存在!";      }?>      result:鍵存在!12345678910111213

array_merge() 函數

把兩個數組合并為一個數組:

<?php$a1=array("red","green");$a2=array("blue","yellow");print_r(array_merge($a1,$a2));?>result:Array ( [0] => red [1] => green [2] => blue [3] => yellow )1234567

array_reverse()

以相反的元素順序返回數組:

<?php$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");print_r(array_reverse($a));?>result:Array ( [c] => Toyota [b] => BMW [a] => Volvo )123456

array_unshift() 函數

用於向數組插入新元素。新數組的值將被插入到數組的開頭。

<?ph$a=array("a"=>"red","b"=>"green");array_unshift($a,"blue");print_r($a);?>result:Array ( [0] => blue [a] => red [b] => green )1234567

array_values

返回一個包含給定數組中所有索引值的數組,但不保留鍵名。

<?php$a=array("Name"=>"Bill","Age"=>"60","Country"=>"USA");print_r(array_values($a));?>result:Array ( [0] => Bill [1] => 60 [2] => USA )123456

hash_equals

可防止時序攻擊的字串比較
比較兩個字串,無論它們是否相等,本函數的時間消耗是恒定的。
本函數可以用在需要防止時序攻擊的字串比較情境中, 例如,可以用在比較 crypt() 密碼雜湊值的情境。

bool hash_equals ( string $known_string , string $user_string )1

參數:
known_string
已知長度的、要參與比較的 string
user_string
使用者提供的字串

傳回值:
當兩個字串相等時返回 TRUE,否則返回 FALSE。

<?php$expected  = crypt('12345', '$2a$07$usesomesillystringforsalt$');$correct = crypt('12345', '$2a$07$usesomesillystringforsalt$');$incorrect = crypt('apple',  '$2a$07$usesomesillystringforsalt$');var_dump(hash_equals($expected, $correct));var_dump(hash_equals($expected, $incorrect));?>result:bool(true)bool(false)123456789101112

in_array() 函數

搜尋數組中是否存在指定的值。

<?php$people = array("Bill", "Steve", "Mark", "David");if (in_array("Mark", $people))  {  echo "匹配已找到";  }else  {  echo "匹配未找到";  } ?>result:匹配已找到1234567891011121314

sprintf() 函數

把百分比符號(%)符號替換成一個作為參數進行傳遞的變數:

<?php$number = 2;$str = "Shanghai";$txt = sprintf("There are %u million cars in %s.",$number,$str);echo $txt;?>result:There are 2 million cars in Shanghai.12345678

str_ireplace()

替換字串中的一些字元(不區分大小寫) str_ireplace(find,replace,string,count)

<?phpecho str_ireplace("WORLD","Shanghai","Hello world!");?>result:Hello Shanghai!12345

strpos

尋找字串在另一字串中第一次出現的位置。

<?phpecho strpos("You love php, I love php too!","php");?>result:912345

str_replace()

以其他字元替換字串中的一些字元(區分大小寫)

<?phpecho str_replace("world","Shanghai","Hello world!");?>result:Hello Shanghai!12345

str_ireplace()
替換字串中的一些字元(不區分大小寫)
str_ireplace(find,replace,string,count)

<?phpecho str_ireplace("WORLD","Shanghai","Hello world!");?>result:Hello Shanghai!12345

substr

返回字串的一部分。

<?phpecho substr("Hello world",6);?>result:world

上面是我整理給大家的PHP常用函數,希望今後會對大家有協助。

相關文章:

PHP狀態模式使用詳解

PHP中如何?Hook機制

PHP中遞迴詳解

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.