Search code examples
phpstringnullexplode

PHP explode on string throws NULL object when accessing the array


I'm trying to explode a string for a video playtime. My Code is:

$viedeolaenge = $file['playtime_string']; // $videolaenge is now string 00:06
var_dump($viedeolaenge); // string(4) "0:06"
$minuten_sekunden = explode(":",$videolaenge);
var_dump($minuten_sekunden); // array(1) { [0]=> string(0) "" }

I've tried to access the array via

    $minuten_sekunden[0] -> returns string(0) ""
    and 
    $minuten_sekunden[1] -> returns NULL

and was now wondering, why this is not working... when I explode the string with seperator ":" and my string is "00:06", then

$minuten_sekunden[0] should return "00"

and

$minuten_sekunden[1] should return "06"

at least in my logic... what am I missing?


Solution

  • Okay, the OP just needs to turn on the display of errors, i.e. turn on error_reporting. Then they would see that the variable is misspelled and therefore a notice appears about an undefined variable.

    Notice: Undefined variable: videolaenge in /in/emNVQ on line 5

    If you correct the variable's spelling then you'll get the desired result:

    <?php
    
    $videolaenge = "00:06";
    var_dump($videolaenge); // string(4) "0:06"
    $minuten_sekunden = explode(":",$videolaenge);
    var_dump($minuten_sekunden); // array(1) { [0]=> string(0) "" }
    

    Live code here

    Note: this isn't really about correct spelling but consistent spelling. So, I just made all three variable references have the spelling indicated in the notice.