在本文中,我们将看到如何使用PHP中的end()函数获取数组中的最后一个元素,并通过示例了解它们的实现。
null
这个 end()函数 是PHP中的一个内置函数,用于查找给定数组的最后一个元素。函数的作用是:将数组的内部指针更改为指向最后一个元素,并返回最后一个元素的值。
语法:
end($array)
参数: 此函数只接受一个参数 $array .它是我们要查找的最后一个元素的数组。
返回值 :成功时返回数组最后一个元素的值,否则在数组为空时失败时返回FALSE。
考虑下面的例子。
Input: array('Ram', 'Shita', 'Geeta')Output: GeetaExplanation: Here, the input array contain many elementsbut output is Geeta i.e, last element of the array as the end() function returns the last element of an array.
例1 :下面的示例演示了PHP中的end()函数。
PHP
<?php // Input array $arr = array ( 'Ram' , 'Shita' , 'Geeta' ); // end function print the last // element of the array. echo end ( $arr ); ?> |
输出 :
Geeta
例2 :此示例演示如何从数组中检索最后一个元素。
PHP
<?php // Input array $arr = array ( '1' , '3' , 'P' ); // end function print the last // element of the array. echo end ( $arr ). "" ; // end() updates the internal pointer // to point to last element as the // current() function will now also // return last element echo current( $arr ); ?> |
输出 :
PP
参考: http://php.net/manual/en/function.end.php
PHP是一种专门为web开发设计的服务器端脚本语言。通过以下步骤,您可以从头开始学习PHP PHP的教程 和 PHP示例 .
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END