Join WhatsApp ChannelJoin Now

Get First Character from String in PHP?

Hi dev,

Today, i show you how to Get First Character from String in PHP?. This article will give you simple example of Get First Character from String in PHP?. you will Get First Character from String in PHP?. In this article, we will implement a Get First Character from String in PHP?.

we will use substr() php function to get first character from string.

you can use this example with laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 version.

So, let’s follow few steps to create example of Get First Character from String in PHP?.

PHP Get First Character from String

index.php

<?php
   
    /* Declare string variable */
    $myString = "Welcome to codeplaners.com!";
        
    /* get First Character of string */
    $char = substr($myString, 0, 1);
        
    /* Echo resulted string */
    echo $char;
   
?>

Output:

W

index.php

<?php
    
    /* Declare string variable */
    $myString = "Welcome to codeplaners.com!";
         
    /* get Last Character of string */
    $char = substr($myStr, -1);
         
    /* Echo resulted string */
    echo $char;
    
?>

Output:

!

PHP Get First and Last Character from String

index.php

<?php
    
    /* Declare string variable */
    $myString = "Welcome to codeplaners.com!";
         
    /* get First and Last Character of string */
    $first = substr($myString, 0, 1);
    $last = substr($myString, -1);
        
    /* Echo resulted string */
    echo $first;
    echo $last;
   
?>

Output:

W
!

I hope it will assist you…

Recommended Posts