所以我有这段代码:
$file= pathinfo($_FILES['screenshot']['name']);
$file_extension=$file['extension'];
if($file_extension !='jpg' or $file_extension !='jpeg'){
$screenshot_error='Please upload an image in jpg format';
}
在我的脑海里,我很困惑为什么即使在我上传的文件是jpg时,此代码在php中返回true的原因。(对不起,我第一次写它返回false)但是当我将'or'更改为'and'时:
$file= pathinfo($_FILES['screenshot']['name']);
$file_extension=$file['extension'];
if($file_extension !='jpg' and $file_extension !='jpeg'){
$screenshot_error='Please upload an image in jpg format';
}
它工作正常。我想知道这种逻辑背后的原因。任何见解都会有所帮助。
您可以使用De Morgan的定律重写逻辑语句
not a or not b -> not (a and b)
not a and not b -> not (a or b)
您想要的不是(a或b),这等效于您的第二个示例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句