foreach 在爆炸后停止
foreach stops after explode
我想知道为什么 foreach 在爆炸后停止,它 returns 只有第一个结果:
$disabled_functions = ini_get('disable_functions');
print_r($disabled_functions);
$disFunctionsNoSpace = str_replace(' ', '', $disabled_functions);
// put them in an array
$disFunctions = explode(',', $disFunctionsNoSpace);
print_r($disFunctions);
/*
var_dump($disFunctions);
OUTPUT
array (size=33)
0 => string 'curl_multi_exec' (length=15)
1 => string ' parse_ini_file' (length=15)
2 => string ' symlink' (length=8)
3 => string ' shell_exec' (length=11)
4 => string ' proc_close' (length=11)
Etc
*/
$this->disFunctions = $disFunctions;
//print_r($this->disFunctions);
// get file content of the uploaded file (renamed NOT the temporary)
$cFile = file_get_contents($this->fileDestination, FILE_USE_INCLUDE_PATH);
//var_dump($cFile);
foreach($this->disFunctions as $kkeys => $vvals)
{
echo $kkeys.' is '.$vvals.'<br />';
if(preg_match('#'.$vvals.'#i', $cFile))
{
echo 'exists<br />';
return true;
//echo count($this->aErrors).'<br />';
//return false;
}
else
{
echo $vvals.'NOT FOUND<br />';
return false;
}
} // end foreach
结果是:0是curl_multi_exec
curl_multi_execNOT FOUND
我想要获取所有键和值的列表。
感谢您的支持
问题是你的循环中有一个 return
。
在您的 foreach
循环中,您有一个 if
/else
块。不管if
是否满足,你return
一个值(true
为if
,false
为else
)。
这 return
终止循环的执行。它还会停止您所在的任何函数(或方法),或者如果您不在函数中,则停止当前脚本。 From the docs:
If called from within a function, the return
statement immediately ends execution of the current function, and returns its argument as the value of the function call. return
also ends the execution of an eval()
statement or script file.
If called from the global scope, then execution of the current script file is ended. If the current script file was include
d or require
d, then control is passed back to the calling file. Furthermore, if the current script file was include
d, then the value given to return
will be returned as the value of the include
call. If return
is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file
or auto_append_file
configuration options in php.ini
, then that script file's execution is ended.
基于 (以及您正在使用 $this
的事实),您的循环在一个方法中。 return
因此会立即停止循环,停止方法,并且 returns true
或 false
视情况而定。它永远不会到达 $this->disFunctions
.
中的第二项
我想知道为什么 foreach 在爆炸后停止,它 returns 只有第一个结果:
$disabled_functions = ini_get('disable_functions');
print_r($disabled_functions);
$disFunctionsNoSpace = str_replace(' ', '', $disabled_functions);
// put them in an array
$disFunctions = explode(',', $disFunctionsNoSpace);
print_r($disFunctions);
/*
var_dump($disFunctions);
OUTPUT
array (size=33)
0 => string 'curl_multi_exec' (length=15)
1 => string ' parse_ini_file' (length=15)
2 => string ' symlink' (length=8)
3 => string ' shell_exec' (length=11)
4 => string ' proc_close' (length=11)
Etc
*/
$this->disFunctions = $disFunctions;
//print_r($this->disFunctions);
// get file content of the uploaded file (renamed NOT the temporary)
$cFile = file_get_contents($this->fileDestination, FILE_USE_INCLUDE_PATH);
//var_dump($cFile);
foreach($this->disFunctions as $kkeys => $vvals)
{
echo $kkeys.' is '.$vvals.'<br />';
if(preg_match('#'.$vvals.'#i', $cFile))
{
echo 'exists<br />';
return true;
//echo count($this->aErrors).'<br />';
//return false;
}
else
{
echo $vvals.'NOT FOUND<br />';
return false;
}
} // end foreach
结果是:0是curl_multi_exec
curl_multi_execNOT FOUND
我想要获取所有键和值的列表。
感谢您的支持
问题是你的循环中有一个 return
。
在您的 foreach
循环中,您有一个 if
/else
块。不管if
是否满足,你return
一个值(true
为if
,false
为else
)。
这 return
终止循环的执行。它还会停止您所在的任何函数(或方法),或者如果您不在函数中,则停止当前脚本。 From the docs:
If called from within a function, the
return
statement immediately ends execution of the current function, and returns its argument as the value of the function call.return
also ends the execution of aneval()
statement or script file.If called from the global scope, then execution of the current script file is ended. If the current script file was
include
d orrequire
d, then control is passed back to the calling file. Furthermore, if the current script file wasinclude
d, then the value given toreturn
will be returned as the value of theinclude
call. Ifreturn
is called from within the main script file, then script execution ends. If the current script file was named by theauto_prepend_file
orauto_append_file
configuration options inphp.ini
, then that script file's execution is ended.
基于 $this
的事实),您的循环在一个方法中。 return
因此会立即停止循环,停止方法,并且 returns true
或 false
视情况而定。它永远不会到达 $this->disFunctions
.