How to Get First Character from String in PHP? - Techvblogs
PHP

How to Get First Character from String in PHP?

In this article, we will implement how to get the first character from a string in PHP.


Smit Pipaliya - Author - Techvblogs
Smit Pipaliya
 

3 weeks ago

TechvBlogs - Google News

In this quick example, PHP gets the first character from the string. In this article, we will implement how to get the first character from a string in PHP. If you have a php question, get the first character from the string example, then I will give a simple example with a solution. I explained to get to the first character from string PHP.

We will use the substr() PHP function to get the first character from the string. I will give you very simple examples to get the first character from a string in PHP.

PHP Get First Character From String

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

Output:

W

PHP Get Last Character From String

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

Output:

!

PHP Get First and Last Character From String

<?php
    
    /* Declare string variable */
    $myString = "Welcome to TechvBlogs.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
!

Thank you for reading this article.

PHP

Comments (0)

Comment


Note: All Input Fields are required.