[英]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.