How to get the last char of a string in PHP?

前端 未结 12 1477
说谎
说谎 2020-12-04 05:24

I need to get the last character of a string. Say I have \"testers\" as input string and I want the result to be \"s\". how can I do that in PHP?

12条回答
  •  没有蜡笔的小新
    2020-12-04 05:52

    From PHP 7.1 you can do this (Accepted rfc for negative string offsets):

    I'll let you guess the output.

    Also, I added this to xenonite's performance code with these results:

    substr() took 7.0334868431091seconds

    array access took 2.3111131191254seconds

    Direct string access (negative string offsets) took 1.7971360683441seconds

提交回复
热议问题