grappler
grappler

Reputation: 557

Create joined string by looping through key value array

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.

Upvotes: 0

Views: 10186

Answers (4)

Terry Seidler
Terry Seidler

Reputation: 2043

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

Upvotes: 1

Sahil Mittal
Sahil Mittal

Reputation: 20753

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;

Upvotes: 2

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

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;
    

Upvotes: 2

Plenka
Plenka

Reputation: 1139

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;

Upvotes: 2

Related Questions