RhymeGuy
RhymeGuy

Reputation: 2112

Why is there no output when using a PHP list function?

I want to assign values of pathinfo function to variables like so:

list($dirname, $basename, $extension, $filename) = pathinfo($path_image);
echo $dirname.$basename.$extension.$filename;

However there is no output.

But if I run only:

print_r(pathinfo($path_image));

I get output like so:

Array ( [dirname] => http://blah.com/images [basename] => image123.jpg [extension] => jpg [filename] => image123) 

Upvotes: 1

Views: 1391

Answers (3)

BlitZ
BlitZ

Reputation: 12168

  1. list() is not a function, it is a language construction.
  2. It does not work with associative arrays. It works with indexed ones.

From php.net:

this is not really a function, but a language construct


list() only works on numerical arrays and assumes the numerical indices start at 0.


To fix that, you may try to ommit result array keys by array_values(), as mentiononed in answer of @anupam:

<?php
$values = array_values(pathinfo($path_image));
list($dirname, $basename, $extension, $filename) = $values;
?>

Upvotes: 5

RhymeGuy
RhymeGuy

Reputation: 2112

Work:

list($dirname, $basename, $extension, $filename) = array_values(pathinfo($wallpaper_image));
echo $dirname.$basename.$extension.$filename;

Upvotes: 2

anupam
anupam

Reputation: 756

pathinfo() returns an associative array. So, your code should work as follows:

list($dirname, $basename, $extension, $filename) = array_values(pathinfo($path_image));

Upvotes: 2

Related Questions