奇怪的行为即使这样,以为自己就是正确的,
$path = "images/a/ba9c59634f8243caa593483433f715ad.jpg";
$filename = pathinfo($path)['basename'];
$real_path = '';
if(strpos($path,"images/a/")>=0){
$real_path = '/app/upload/images/a/'.$filename;
echo 1;
}
if(strpos($path,"images/b/")!>=0){
$real_path = '/app/upload/images/b/'.$filename;
echo 2;
}
结果是
12
然后翻看文档,和百度了以下终于理解了,较为正确的写法是
$path = "images/a/ba9c59634f8243caa593483433f715ad.jpg";
$filename = pathinfo($path)['basename'];
$real_path = '';
if(strpos($path,"images/a/")!==false){
$real_path = '/app/upload/images/a/'.$filename;
echo 1;
}
if(strpos($path,"images/b/")!==false){
$real_path = '/app/upload/images/b/'.$filename;
echo 2;
}
1
strpos是查找字符串中是否出现子窜;存在返回第一次存在的位置,不存在返回false;但是位置也有0,0!==false , !== 时表示返回值,只要不是false就行,可以专是0。
|