【问题标题】:PHP variable in foreach loopforeach 循环中的 PHP 变量
【发布时间】:2014-05-27 16:46:17
【问题描述】:

我试图在 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;

我用谷歌搜索了没有找到任何解决方案。

【问题讨论】:

    标签: php arrays variables loops foreach


    【解决方案1】:

    在循环开始之前定义$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;
    

    【讨论】:

    • 你知道为什么必须在循环之前预定义吗?对我来说似乎很奇怪,它几乎就像一个变量范围问题。
    • 不是变量范围问题,而是在每次迭代中,他都将空白值设置为变量
    【解决方案2】:

    在这两种情况下,变量都需要在 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;
    

    【讨论】:

      【解决方案3】:

      第一个问题

      • 需要在循环外定义 $source 变量。

      第二期

      • 非常类似于首先你需要在循环外定义变量然后连接 循环内。您正在循环内部进行操作,这就是为什么它会被覆盖并获得最后一个值的原因。

        $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;
        

      【讨论】:

        【解决方案4】:

        第一条消息undefined variable $source 表示尚未定义名为$source 的变量。该代码将在不定义变量源的情况下工作,但这不是要走的路;)

        虽然 PHP 不需要变量声明,但它确实推荐 它是为了避免一些安全漏洞或错误,其中一个 会忘记给他稍后将使用的变量赋值 剧本。 PHP 在未声明变量的情况下做什么是问题 一个非常低级别的错误,E_NOTICE,甚至没有被报告 默认,但手册建议在开发期间允许。

        (PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset")

        至于您的第二个问题.. 您正在重新定义 $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/en/language.variables.basics.php

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-06
          • 2012-12-20
          • 2013-09-26
          • 1970-01-01
          • 2015-06-02
          • 2011-05-08
          • 1970-01-01
          相关资源
          最近更新 更多