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.
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.
$string = "quiz lancer";
$uppercase = strtoupper($string); // $uppercase will be "QUIZ LANCER"
#5. strtolower()
This function converts a string to a lowercase. Means capital letters to small letters. It is the inverse of strtoupper() function.
$string = "QUIZ LANCER";
$lowercase = strtolower($string); // $lowercase will be "quiz lancer"
11+ Unlock Mastery PHP Strings MCQ Questions and Answers
#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:
string mysqli_real_escape_string (mysqli $link, string $escapestr)
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:
// Establishing a connection to the MySQL database
$link = mysqli_connect("localhost", "username", "password", "database_name");
// Assuming $input contains the user input to be escaped
$input = $_POST['userinput'];
// Escaping the input before using it in a query
$escapedinput = mysqli_real_escape_string($link, $input);
// Using the escaped input in a query
$query = "SELECT * FROM table_name WHERE column = '$escapedinput'";
$result = mysqli_query($link, $query);
In the example above, the mysqli_real_escape_string function is used to escape the user input stored in $input before including it in the SQL query. This helps prevent SQL injection attacks by ensuring that special characters are properly escaped before being used in the query.
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
<?php
$str = " Quiz Lancer ";
// Remove whitespace from both sides of the string
$trimmed_str = trim($str);
echo "Original string: " . $str;
echo "Trimmed string: " . $trimmed_str;
?>
This code will output the following:
Original string: Quiz Lancer
Trimmed string: Quiz Lancer
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
<?php
$str = " Quiz Lancer ";
// Remove whitespace from starting (left) side of the string
$trimmed_str = trim($str);
echo "Original string: " . $str;
echo "Trimmed string: " . $trimmed_str;
?>
This code will output the following:
Original string: Quiz Lancer
Trimmed string: Quiz Lancer
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
<?php
$str = " Quiz Lancer ";
// Remove whitespace from end (right) side of the string
$trimmed_str = trim($str);
echo "Original string: " . $str;
echo "Trimmed string: " . $trimmed_str;
?>
This code will output the following:
Original string: Quiz Lancer
Trimmed string: Quiz Lancer
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 string | Removes spaces only from both beginning of string | Removes 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” |
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.
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
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?](https://i0.wp.com/quizlancer.com/wp-content/uploads/2024/06/split-a-string-into-an-array.jpg?resize=576%2C1024&ssl=1)
![Which function can be used to split a string into an array based on a delimiter?](https://i0.wp.com/quizlancer.com/wp-content/uploads/2024/06/split-a-string-into-an-array.jpg?resize=576%2C1024&ssl=1)
Give your answer in a comment below.