Make Your Code Cleaner and More Concise With the PHP Function Ltrim
The php function ltrim is a built-in string trimmer that removes white space and pre-defined characters from both sides of a given string. It accepts one mandatory parameter and an optional list of characters to be removed from the left side. The function returns the modified string.
The trim function is commonly used when there are many blank spaces at the beginning of an input string, for example, a URL or a name. However, the problem with this approach is that it also cuts off any characters within the string itself, such as spaces and tabs, and the result would be a completely different text than the original input string. This is why you should always be aware of the consequences of using this function.
Thankfully, there is a better way to handle this problem. The trim function has a similar alternative known as rtrim, which does exactly the opposite of ltrim, by trimming any white space or pre-defined characters from the end of the string.
Learn how to make your code cleaner and more concise with php function ltrim and other powerful functions. Get access to exclusive videos, interactive demos, and hands-on projects. Get now with O'Reilly Learn.
If you are interested in learning about other php functions and coding practices, check out our full range of online courses.
Learn to build mobile apps, websites, and web applications in just a few weeks with our beginner-friendly courses.
This article is part of our Full Stack Web Developer - MEAN Stack Master's Program. To get started, click the button below to fill out the form.