Monday 16 July 2018

How to Get the Last Character in a String with PHP

How to Get the Last Character in a String with PHP


There a number of ways to get the last character in a string by using PHP. The simplest way I find however is to use the substr() function like so:

  1. $str = "This is a string";  
  2. echo substr($str, -1);  
The above example would return ‘g’. By passing a negative value as the second parameter we can tell PHP to start at the nth character from the end.

0 comments:

Post a Comment