Reputation: 283
I need to make following in one line, i am looking for method of making similar code one line not to solve this particular example.
$file_path = pathinfo($_SERVER["SCRIPT_NAME"]);
$file_name = $file_path["filename"];
e.g.
$file_name = pathinfo($_SERVER["SCRIPT_NAME"])["filename"];
Upvotes: 2
Views: 48
Reputation: 158130
Unfortunately PHP's syntax does not support this for versions lower than PHP5.5. As of PHP5.5 you can just write:
echo pathinfo($path)['filename'];
If you are working with a version < 5.5, I would suggest to write a custom function:
function array_get($key, $array) {
if(!array_kex_exists($key, $array)) {
throw new Exception('$array has no index: ' . $key);
}
return $array[$key];
}
Then use it:
$filename = array_get('filename', pathinfo($path));
Upvotes: 0
Reputation: 12836
Array dereferencing is only possible from php 5.5. If you have an earlier version, sadly, it will not be possible.
However you could try using list
, which will assign all the array elements in pathinfo
to individual variables. If you order your variables correctly, $file_name
will have what you need.
Upvotes: 2