In this tutorial, we are going to learn about how to get the first 2 characters of a string in PHP.
In PHP, strings are the sequence of characters, where the index of the first character is
0, the second character is
1, third character is
3, last character index is
To get the first 2 characters of a string, we can use the built-in substr() function in PHP.
Here is an example:
<?php echo substr("Google", 0, 2); ?>
In the example above, we have passed
0, 2 as an arguments to the
substr() function. So, it begins the extraction at index
1 and extracts before the 2rd character of a string.
Syntax : substr(string, start, length)
The second argument is
start index and third argument is
length that means how many characters we need to extract from the