Top 5 PHP String Functions You Should Know as a Developer

PHP String Functions

Before learning PHP String Functions we should know what a String is. A string is a sequence of characters, like “I am a Developer”. This means I am a Developer which we have defined in double quotes as a String.

Join to Become PHP Web Developer - Future Scope Career Guidance

WhatsApp Group Join Now
Telegram Group Join Now
Follow On Instagram Follow Us

Why do we use PHP string functions?

  • Make code reusable, repeated processes are done as a function to avoid rewriting code each time.
  • Make code more readable and easy to debug and test.
  • We use functions to save repetitive functionality, which is needed throughout the program.

Read more from Quora about why functions in PHP are used.


List of PHP String Functions

#1. str_word_count()

This function is used to count the number of characters in a String. Let’s take one example for better understanding.
<?php echo str_word_count(“I am a Quizlancer”); ?>
Result will be 4. In the above example, in the double quote, there are 4 words, I am a Quizlancer, so the result is 4.

#2. strlen()

This function returns the Lenght of the String.

<?PHP echo strlen(“Quizlancer”); ?>

The result will be 10. Here Q, u, i, z, l, a, n, c, e, and r are 10 characters, so the result is 10.

Now see one more example for a better understanding.

Now, please pay attention here, the result is 12. Please don’t confuse, let me explain.
“A Quizlancer” here, A, Q, u, i, z, l, a, n, c, e and r. These are 11 characters, you think the result should be 11, but the exact result is 12. Because there is a space between A and Quizlancer word. Then one space is considered a character in a String. So that the result is 12.

#3. strrev()

strrev() function used to reverse the string.
<?php echo strrev(“I am Quiz Lancer”); ?>
Result will be like as below
recnaL ziuQ ma I
recnaL ziuQ ma I is a “I am Quiz Lancer” string, just in the reverse direction or reverse order.

#4. strtoupper()

strtoupper() function converts a string to uppercase. Means small letter to upper letter.

#5. strtolower()

This function converts a string to a lowercase. Means capital letters to small letters. It is the inverse of strtoupper() function.

#6. mysqli_real_escape_string

The ‘mysqli_real_escape_string’ is a PHP function used to escape special characters in a string before sending it as a parameter in a MySQL query executed with the MySQLi extension.

Here’s the basic syntax of the mysqli_real_escape_string function:

Parameters:

  • $link: A mysqli connection object representing the connection to the MySQL database.
  • $escapestr: The string to be escaped.

The mysqli_real_escape_string function takes the $link parameter to ensure that the function is aware of the character set used by the database connection. To use mysqli_real_escape_string, you need to establish a connection to the database using the mysqli_connect function or create a mysqli object and pass it as the $link parameter.

Here’s an example that demonstrates the usage of mysqli_real_escape_string:

It’s important to note that mysqli_real_escape_string should be used in conjunction with prepared statements or parameterized queries for more secure and reliable protection against SQL injection.

#7. trim()

This function removes whitespace (spaces, tabs, newlines) from the beginning and end of a string. This means this function removes spaces from both sides (Left and Right). It’s used for cleaning up user input or ensuring consistent data formatting.

Let’s see one example for a better understanding

This code will output the following:

Trimmed string removed spaces from start and end.

#8. ltrim()

This function is used to remove whitespace from the beginning (left) only, not from the end (right) side.

Let’s see one example for a better understanding

This code will output the following:

Trimmed string removed spaces from the start (left) only not from the end (right). At end (right) spaces as it is there.

#9. rtrim()

This function is used to remove whitespace from the right (end) only, not from the left (start) side.

Let’s see one example for a better understanding

This code will output the following:

Trimmed string removed spaces from the end (right) side only not from the start (left) side. At start (left) spaces as it is there.


What is Difference in trim(), ltrim() and rtrim() functions

trim()ltrim()rtrim()
Removes spaces from both beginning and end of stringRemoves spaces only from both beginning of stringRemoves spaces only from end of string
$str = ” Quiz Lancer “;
echo trim($str);
// Outputs: “Quiz Lancer”
$str = ” Quiz Lancer “;
echo ltrim($str);
// Outputs: “Quiz Lancer “
$str = ” Quiz Lancer “;
echo rtrim($str);
// Outputs: ” Quiz Lancer”
Difference between trim(), ltrim() and rtrim() functions

FAQ on PHP String Functions

Is mysqli_real_escape_string safe? – PHP

Yes, when used correctly, mysqli_real_escape_string is a safe method to protect against SQL injection attacks in PHP. It escapes special characters in a string, making it safe to include the escaped string in an SQL query.

String Functions are essential and we must know that function as a Full Stack Developer. At the time of the interview, most of the questions are based on String Functions. And most of the students fail in the interview because of confusion about String functions. There are several string functions that have a little bit of difference. For better understanding and preparation for interviews, I have created Quiz on PHP String Functions Interview Questions. Solve quizzes several times so you are ready for the interview.

0%
7
Created on By quizlancer

PHP String Functions

1 / 10

1. Which function is used to remove whitespace or other predefined characters from the right side of a string?

2 / 10

2. Which functions return the length of the string?

3 / 10

3. Which function is used to concatenate two strings in PHP?

4 / 10

4. The ______ function converts a string to lowercase.

5 / 10

5. Which function is used for repeating a string a specified number of times?

6 / 10

6. Which function is used for reversing a string?

7 / 10

7. ______ function counts the number of words in a string.

8 / 10

8. When two strings are exactly equivalent Strcmp ( ) returns what?

9 / 10

9. Which function is used to remove characters from both sides of a string?

10 / 10

10. Which is a function that compares two strings.

Your score is

The average score is 61%

0%

Solve Quiz on PHP String Functions repeatedly until you understand the exact use of String functions in development.


Watch This Video for a Better Understanding of PHP String Functions

PHP String Functions with Examples

Follow Us on Social Media for Daily Questions on PHP String Functions

Follow Quiz Lancer Telegram Channel to solve daily interview questions. We share important interview questions which are asked in the interview. Most of the students get good values from this channel and got selected as a developer.


Trending Topics for You


Which function can be used to split a string into an array based on a delimiter?

Give your answer in a comment below.

89 / 100
Scroll to Top