什么是$ variable [count($ variable)-1]在做什么?

Cra*_*iem 0 php count

这是一个简单的问题,但无论我在哪里,我似乎无法弄清楚它是如何工作的.我相信这是从$ variable开始的第一个字符,但是怎么count($variable)-1做呢?

$variable[count($variable)-1]
Run Code Online (Sandbox Code Playgroud)

完整代码:

$fileType  = explode('.',$_FILES['Filedata']['name']);
$fileName = str_ireplace('.jpg', '', $_FILES['Filedata']['name']);
$targetFile =  str_replace('//','/',$targetPath) . $fileName .'.'.$fileType[count($fileType)-1];
Run Code Online (Sandbox Code Playgroud)

小智 5

count($variable) 返回数组中元素的数量,但PHP中的数组索引是从零开始的:也就是说,10元素数组具有索引为0-9的元素.

因此,$variable[count($variable) - 1]获取数组中的最后一个元素.

虽然,这可以通过以下方式完成end():end($variable) == $variable[count($variable-1)].