Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 2 down vote accepted

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;
share|improve this answer

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;
share|improve this answer

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;
    
share|improve this answer

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" and "Notice: Undefined index")

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.