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
 

10 months 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.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

PHP

Comments (0)

Comment


Note: All Input Fields are required.