0

I am trying to use a variable in a foreach loop but I am getting strange results. 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.

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

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
grappler
  • 527
  • 5
  • 17

4 Answers4

2

Define $source an d $source1 before the loops start.

 $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;
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
2

In both cases the variables need to be defined outside the foreach loop:

$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;
Plenka
  • 1,099
  • 7
  • 17
2

First problem

  • Need to define the $source variable outside the loop.

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;
    
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
1

The first message undefined variable $source means that the variable named $source has not been defined yet. 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. 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: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset")

As for your second problem.. You're redefining $source2 every iteration of the loop. Simply move $source2 so that it is defined on the line above the 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

Community
  • 1
  • 1
Terry Seidler
  • 2,043
  • 15
  • 28