我對(duì) if 條件中語(yǔ)句的含義及其目的有疑問。我在下面發(fā)布了完整的 PHP 代碼。這是我目前正在研究的另一個(gè)人的代碼,我的問題是關(guān)于特定的代碼行(請(qǐng)參閱我的問題的下一段)。<?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>在這個(gè) php 代碼中,我不太確定該語(yǔ)句是什么if($filesread[$withoutExt] !== true)意思。 if($filesread[$withoutExt] !== true) {//code to be excute}我不知道是什么$filesread[$withoutExt]。我所知道的是$filesread = array();并且$withoutExt是不帶擴(kuò)展名的文件名。我的猜測(cè)是程序員試圖使用$withoutExt 作為從$filesread數(shù)組中獲取值的鍵,但 $filesread是一個(gè)空數(shù)組。他如何從空數(shù)組中獲取值?有誰(shuí)理解$filesread[$withoutExt]這個(gè) if 語(yǔ)句的含義和目的?非常感謝!
1 回答

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