我對 if 條件中語句的含義及其目的有疑問。我在下面發布了完整的 PHP 代碼。這是我目前正在研究的另一個人的代碼,我的問題是關于特定的代碼行(請參閱我的問題的下一段)。<?php $dir = '../assets/videos/'; if(is_dir($dir)) { $filesread = array(); $dh = opendir($dir); print_r($filesread); while(($file = readdir($dh)) !== false) { if($file != '.' && $file != '..') { //returns all file names wihout extension $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file); if($filesread[$withoutExt] !== true) { $hasSQL = false; foreach($films as $f) { if($f['file_name'] == $withoutExt) { echo ' <div class="row"> <div class="col-xs-8"> <a href="'.$dir.$f['file_name'].'.mp4" target="_blank">'.$withoutExt.'</a> </div>在這個 php 代碼中,我不太確定該語句是什么if($filesread[$withoutExt] !== true)意思。 if($filesread[$withoutExt] !== true) {//code to be excute}我不知道是什么$filesread[$withoutExt]。我所知道的是$filesread = array();并且$withoutExt是不帶擴展名的文件名。我的猜測是程序員試圖使用$withoutExt 作為從$filesread數組中獲取值的鍵,但 $filesread是一個空數組。他如何從空數組中獲取值?有誰理解$filesread[$withoutExt]這個 if 語句的含義和目的?非常感謝!
1 回答

HUX布斯
TA貢獻1876條經驗 獲得超6個贊
$filesread
是一個包含文件名的數組,沒有擴展名作為鍵。對于一個假設已經列出的項目,其標志為 true。分配發生在輸出之后:
$filesread[$withoutExt] = true;
我的猜測是可能有多個文件名包含不同的擴展名。也許每部電影的字幕或縮略圖文件。而且您不想多次列出它們。
因此檢查:
$filesread[$withoutExt] !== true
對于無擴展鍵,數組不包含 true 值。
然而,表達式在這里可以很容易地寫成:
!isset($filesread[$withoutExt])
但由于這是代碼的摘錄,因此該數組可能包含在其他地方分配的其他值。
在第一次運行時,數組鍵將不存在,因此在檢查值為 true 時可能會觸發警告,因此您可能應該換成更明確的:
!isset($filesread[$withoutExt]) && ($filesread[$withoutExt] !== true)
- 1 回答
- 0 關注
- 121 瀏覽
添加回答
舉報
0/150
提交
取消