简体   繁体   English

如何修复未定义的偏移量:1

[英]How to fix undefined offset: 1

How can I solve this issue (undefined offset: 1 at line 30):我该如何解决这个问题(未定义的偏移量:第 30 行的 1):

$imagesarray = array();
        while($file = readdir($handle)){
            if($file !== '.' && $file !== '..'){
                $fileArr = explode('.', $file );
<<line 30>>             if ( $fileArr[1] == 'jpg' || $fileArr[1] == 'jpeg' || $fileArr[1] == 'png' || $fileArr[1] == 'gif' ){
                    array_push($imagesarray,$file);
                }
            }
        }

I want to upgrade to php8 but gets this in php 7,4 (php8 displays a slightly different outout)我想升级到 php8 但在 php 7,4 中得到这个(php8 显示略有不同的输出)

Can be fixed with this:可以用这个修复:

if (in_array($fileArr[1] ?? '',['jpg','jpeg','png','gif'],true)){

$fileArr[1]?? '' $fileArr[1]?? '' prevents undefined error $fileArr[1]?? ''防止未定义的错误

in_array keeps the if statement short & clean in_array使 if 语句简洁明了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM