[英]PHP variable in foreach loop
我试图在foreach循环中使用变量,但结果却很奇怪。 第一个foreach循环可以正常工作,但会发出未定义变量的通知,而在第二个版本中,则不会发出通知,但只会返回数组中的最后一项。
$formats = array(
'application/x-mpegurl' => 'hls',
'video/webm' => 'webm',
'video/mp4' => 'mp4',
'video/ogg' => 'ogg',
'video/flash' => 'flash',
);
// Works perfectly but there a undefined variable $source
foreach( $formats as $format => $src ){
if ( !empty( $src ) ) {
$source .= '<source type="' . $format . '" src="' . $src . '">';
}
}
echo $source;
// Returns only the last item in the variable but there is no undefined variable
foreach( $formats as $format => $src ){
$source2 = '';
if ( !empty( $src ) ) {
$source2 .= '<source type="' . $format . '" src="' . $src . '">';
}
}
echo $source2;
我用谷歌搜索解决方案没有找到任何。
在循环开始之前定义$source
和$source1
。
$source = "";
// loop starts here
完整的代码:
$source = "";
foreach( $formats as $format => $src ){
if ( !empty( $src ) ) {
$source .= '<source type="' . $format . '" src="' . $src . '">';
}
}
echo $source;
$source2 = '';
foreach( $formats as $format => $src ){
if ( !empty( $src ) ) {
$source2 .= '<source type="' . $format . '" src="' . $src . '">';
}
}
echo $source2;
在这两种情况下,都需要在foreach循环之外定义变量:
$formats = array(
'application/x-mpegurl' => 'hls',
'video/webm' => 'webm',
'video/mp4' => 'mp4',
'video/ogg' => 'ogg',
'video/flash' => 'flash',
);
// Works perfectly but there a undefined variable $source
$source = '';
foreach( $formats as $format => $src ){
if ( !empty( $src ) ) {
$source .= '<source type="' . $format . '" src="' . $src . '">';
}
}
echo $source;
// Returns only the last item in the variable but there is no undefined variable
$source2 = '';
foreach( $formats as $format => $src ){
if ( !empty( $src ) ) {
$source2 .= '<source type="' . $format . '" src="' . $src . '">';
}
}
echo $source2;
第一个问题
第二期
与首先非常相似,您需要在循环外定义变量,然后在循环内连接。 您正在循环内部进行操作,这就是为什么它被覆盖并获得最后一个值的原因。
$source = ''; foreach( $formats as $format => $src ){ if ( !empty( $src ) ) { $source .= '<source type="' . $format . '" src="' . $src . '">'; } } echo $source; $source2 = ''; // Returns only the last item in the variable but there is no undefined variable foreach( $formats as $format => $src ){ if ( !empty( $src ) ) { $source2 .= '<source type="' . $format . '" src="' . $src . '">'; } } echo $source2;
第一条消息undefined variable $source
表示尚未定义名为$source
的变量。 代码可以在不定义变量源的情况下工作,但这不是可行的方法;)
尽管PHP不需要变量声明,但它确实推荐这样做,以避免某些安全漏洞或bug,在这些漏洞或bug中,人们可能忘记为变量提供一个值,而该变量将在脚本的后面使用。 在未声明变量的情况下,PHP会发出一个非常低级的错误E_NOTICE,默认情况下甚至不会报告该错误,但《手册》建议在开发过程中允许这样做。
( PHP:“通知:未定义的变量”,“通知:未定义的索引”和“通知:未定义的偏移量” )
至于第二个问题..您将在循环的每个迭代中重新定义$source2
。 只需移动$source2
以便在foreach
上方的行中对其进行定义。
// Returns only the last item in the variable but there is no undefined variable
$source2 = ''; // MOVED THIS LINE
foreach( $formats as $format => $src ){
if ( !empty( $src ) ) {
$source2 .= '<source type="' . $format . '" src="' . $src . '">';
}
}
在PHP手册中阅读有关定义变量的更多信息: http : //www.php.net/manual/zh/language.variables.basics.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.