PHP Get First Character from String Example

03-Apr-2023

.

Admin

PHP Get First Character from String Example

Hi Dev,

This example is focused on php get first Character from string example. Here you will learn how to get first Character from string in php?. let’s discuss about get first Character from string in php example. In this article, we will implement a how to use function to get first Character from string in php? .

There are tow example to get First Character from strings in PHP. in this example, we will use to substr() and mb_substr() function to get First Character. so Let's both the following example with output.

Example 1: Get First Character from String


index.php

<?php

$str = "Hello World";

//Get first character from a string

$result = substr($str,0,1);

echo $result;

?>

Output:

H

Example 2: Get First Character from String

index.php

<?php

$str = "Welcome to Nicesnippets.com";

//Get first character from a string

$result = mb_substr($str,0,1);

echo $result;

?>

Output:

W

I hope it could help you...

#PHP