[英]PHP variable in foreach loop
I am trying to use a variable in a foreach loop but I am getting strange results. 我试图在foreach循环中使用变量,但结果却很奇怪。 The first foreach loop work fine but gives a notice of a undefined variable and in the second version there is no notice but it only returns the last item in the array.
第一个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;
I have googled for solutions have not found any. 我用谷歌搜索解决方案没有找到任何。
Define $source
an d $source1
before the loops start. 在循环开始之前定义
$source
和$source1
。
$source = "";
// loop starts here
Complete code: 完整的代码:
$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;
In both cases the variables need to be defined outside the foreach loop: 在这两种情况下,都需要在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;
First problem 第一个问题
Second issue 第二期
Pretty similar to first u need to define the variable outside the loop and then concate inside the loop. 与首先非常相似,您需要在循环外定义变量,然后在循环内连接。 You are doing inside the loop which is why its getting over-written and getting the last value.
您正在循环内部进行操作,这就是为什么它被覆盖并获得最后一个值的原因。
$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;
The first message undefined variable $source
means that the variable named $source
has not been defined yet. 第一条消息
undefined variable $source
表示尚未定义名为$source
的变量。 The code will work without defining the variable source, but it's not the way to go ;) 代码可以在不定义变量源的情况下工作,但这不是可行的方法;)
Although PHP does not require variable declaration, it does recommend it in order to avoid some security vulnerabilities or bugs where one would forget to give a value to a variable that he will use later in the script.
尽管PHP不需要变量声明,但它确实推荐这样做,以避免某些安全漏洞或bug,在这些漏洞或bug中,人们可能忘记为变量提供一个值,而该变量将在脚本的后面使用。 What PHP does in the case of undeclared variables is issue a very low level error, E_NOTICE, one that is not even reported by default, but the Manual advises to allow during development.
在未声明变量的情况下,PHP会发出一个非常低级的错误E_NOTICE,默认情况下甚至不会报告该错误,但《手册》建议在开发过程中允许这样做。
( PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" ) ( PHP:“通知:未定义的变量”,“通知:未定义的索引”和“通知:未定义的偏移量” )
As for your second problem.. You're redefining $source2
every iteration of the loop. 至于第二个问题..您将在循环的每个迭代中重新定义
$source2
。 Simply move $source2
so that it is defined on the line above the foreach
. 只需移动
$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 . '">';
}
}
Read more on defining variables in the PHP manual: http://www.php.net/manual/en/language.variables.basics.php 在PHP手册中阅读有关定义变量的更多信息: http : //www.php.net/manual/zh/language.variables.basics.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.