All TalkersCode Topics

Follow TalkersCode On Social Media

Get First Character Of String PHP

Last Updated : Jul 1, 2023

Get First Character Of String PHP

In this article we will show you the solution of get first character of string php , we will learn about process to collect first character of the provided string using php.

To get desired result you have to use substr() method in php.

It is php built-in function when you have to cut out part of string from one string this method helps widely.

While using this function it needs some required information from you such as to be extracted string length and start index number with the source string.

Step By Step Guide On Get First Character Of String PHP :-

Foremost step you need to define echo statement, we know that help you to display result of output at terminal.

Then nearly specify substr() method with needed inputs of three parameters.

The exact syntax is: substr(“String”,start-index,length of sub string).

To achieve correct result might provide input as per rule with appropriate inputs. We below given example code with sample string.

echo substr("Prawin",0,1);
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
  3. First we defined echo property with substr() method within method we sets source string as ‘Prawin’, start length mostly same to value ‘0’ and here we need to get first character only so we gave length of sub string as value ‘1’.
  4. Hope you get clear sketch of requirements and process, so you can change input based on your wish.
  5. If you not get output properly verify double time chance to occur mistake at definition of input. If you run above given example without modification you get answer as first letter of ‘Prawin’.
  6. Using this concept you can obtain any part of string that means it can contain three characters or two characters and in the middle or end of string. These are possible cases to get like these output you have to change the last two parameter depends on your requirement.
  7. Instead echo() you can prefer print() method if you want and that not going to affects your result. Still some type of input exist you can try those along with it to master your mind in this concept.

Conclusion :-

In conclusion, now we are able know how to attain first character of string in php.

When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.

Otherwise sometimes you will get error so ensure it if you get error. When we execute this program on browser it will print letter ‘P’ output.

For instance you change the string to ‘Kiran’ you get output ‘K’. Besides with string ‘Kiran’, you modified third input value to ‘2’ you get output ‘Ki’.

I hope this article on get first character of string php helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials