久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      一定要改掉 這5個PHP編程中的不良習慣!

      一定要改掉 這5個PHP編程中的不良習慣!

      【相關學習推薦:php圖文教程

      這5個PHP編程中的不良習慣,一定要改掉 PHP世界上最好的語言!

      測試循環(huán)前數(shù)組是否為空?

      $items = [];// ...if (count($items) > 0) {    foreach ($items as $item) {        // process on $item ...     }}復制代碼

      foreach循環(huán)或數(shù)組函數(shù)(array_*)可以處理空數(shù)組。

      • 不需要先進行測試
      • 可以減少一層縮進
      $items = [];// ...foreach ($items as $item) {    // process on $item ...}復制代碼

      將方法的所有內容封裝在if語句中

      function foo(User $user) {    if (!$user->isDisafunction foo(User $user) {    if (!$user->isDisabled()) {        // ...         // long process         // ...     } }bled()) {        // ...         // long process         // ...     } }復制代碼

      這不是特定于PHP的,但我經(jīng)??吹剿?。你可以通過提前返回,來減少縮進級別的極簡代碼! 該函數(shù)的所有“有用”主體現(xiàn)在處于第一個縮進級別

      function foo(User $user) {    if ($user->isDisabled()) {        return;     }    // ...     // long process     // ...}復制代碼

      多次調用isset方法

      $a = null; $b = null; $c = null;// ...if (!isset($a) || !isset($b) || !isset($c)) {    throw new Exception("undefined variable"); }// orif (isset($a) && isset($b) && isset($c) {    // process with $a, $b et $c}// or $items = [];//...if (isset($items['user']) && isset($items['user']['id']) {    // process with $items['user']['id']}復制代碼

      我們經(jīng)常需要檢查是否已定義變量(而不是null)。 在PHP中,我們可以使用isset函數(shù)來做到這一點。而且該函數(shù)一次可以接受多個參數(shù)!

      $a = null; $b = null; $c = null;// ...if (!isset($a, $b, $c)) {    throw new Exception("undefined variable"); }// orif (isset($a, $b, $c)) {    // process with $a, $b et $c}// or $items = [];//...if (isset($items['user'], $items['user']['id'])) {    // process with $items['user']['id']}復制代碼

      echo方法和sprintf結合使用

      $name = "John Doe";echo sprintf('Bonjour %s', $name);復制代碼

      這段代碼可能在微笑,但是我碰巧寫了一段時間。而且我仍然看到很多!除了結合echosprintf,我們可以簡單地使用printf方法。

      $name = "John Doe"; printf('Bonjour %s', $name);復制代碼

      通過組合兩種方法檢查數(shù)組中鍵的存在

      $items = [    'one_key' => 'John',    'search_key' => 'Jane', ];if (in_array('search_key', array_keys($items))) {    // process}復制代碼

      最后一個錯誤我看到的往往是聯(lián)合使用in_arrayarray_keys。所有這些都可以使用array_key_exists替換。

      $items = [    'one_key' => 'John',    'search_key' => 'Jane', ];if (array_key_exists('search_key', $items)) {    // process}復制代碼

      我們還可以使用isset來檢查值是否是null。

      if (isset($items['search_key'])) {    // process}復制代碼

      感謝您的閱讀,如果對您有幫助,歡迎關注"CRMEB"掘金號。碼云上有我們開源的商城項目,知識付費項目,均是基于PHP開發(fā),學習研究歡迎使用,關注我們保持聯(lián)系!

      相關學習推薦:php編程(視頻)

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號