1)String Concatenation Operator
To concatenate two string variables together, use the dot (.) operator -
Example:
<?php
$string1="Welcome to";
$string2="qawithexperts.com";
echo $string1 . " " . $string2;
?>
2)String Length function - strlen() function
The strlen() function is used to find the length of a string.
Let's find the length of our string " qawithexperts ":
Example:
<?php
echo strlen("qawithexperts");
?>
This will produce the following Output - 13
3)Search for a string or character within a string - strpos() function
The strpos() function is used to search for a string or character within a string.
If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE.
Let's see if we can find the string "qawithexperts" in our string -
Example:
<?php
echo strpos("Welcome to qawithexperts","qawithexperts");
?>
This will produce the following output -11
4)A string from the elements of an array - implode()
It is usually the simplest way to break up a string into an array.
implode() Returns a string from the elements of an array
example:
<?php
$my_array = array('Welcome','to','QA With Experts');
echo implode(" ",$my_array);
?>
Output: Welcome to 'QA With Experts
5) explode() function
It is opposite to implode function.
example:
<?php
$qa_numbers_list='one,two,three,four,five,six';
$qa_numbers=explode(",",$qa_numbers_list);
print_r($qa_numbers);
?>
Output:
Array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six )
6) To Calculates the MD5 hash of a string -md5()
It is used to Calculate the md5 hash of a string
Example:
<?php
$qa_var = "qawithexperts";
echo md5($my_var);
?>
Output: c184d8d66ff872c3e6076a92f4ad2108
7)A string formatted as a currency string - money_format()
The money_format() function It is used to display Formats a number as a currency string
Example:
Demo for formatting the Rupee currency (Indian Rupee - INR)
$qa_amount = '700000';
setlocale(LC_MONETARY, 'en_IN');
$qa_amount = money_format('%!i', $qa_amount);
echo $qa_amount;
Output:
7,00,000.00
8)Inserts HTML line breaks in front of each newline in a string - nl2br()
Example:
<?php
echo nl2br("Welcome to QA With Experts.\nHi All.");
?>
Output:
Welcome to QA With Experts.
Hi All.
9)Formats a number with grouped thousands - number_format()
It is used to Formats a number with grouped thousands
Example:
<?php
$qa_number = 2345.67;
$qa_english_format_number = number_format($number);
?>
Output: 2,345
10)Replaces some characters in a string (case-sensitive) - str_replace()
Example:
Replace the characters "PHP" in the string "Welcome to PHP" with "qawithexperts.com":
<?php
echo str_replace("PHP"," qawithexperts.com","Welcome to PHP");
?>
Output: Welcome to qawithexperts.com
11)Removes whitespace or other characters from both sides of a string - trim()
It is used to remove the white spaces and other predefined characters from the left and right sides of a string.
Example:
<?php
$qa_var=' Welcome to qawithexperts.com ';
echo trim($qa_var);
?>
Output: Welcome to qawithexperts.com
12)Converts the first character of a string to uppercase - ucfirst()
It is used to convert the first character of a string to uppercase.
Example:
<?php
$qa_var='welcome to qawithexperts.com';
echo ucfirst($qa_var);
?>
Output: Welcome to qawithexperts.com
13)Converts the first character of each word in a string to uppercase -ucwords()
It is used to convert the first character of each word in a string to uppercase.
Example:
<?php
$qa_var='welcome to qawithexperts.com';
echo ucwords($qa_var);
?>
Output: Welcome To Qawithexperts.com
14)Converts a string to lowercase letters - strtolower()
It is s used to convert all alphabetic characters of a string to lowercase.
Example:
<?php
$qa_var='WELCOME TO QAWITHEXPERTS.COM';
echo strtolower($qa_var);
?>
Output: welcome to qawithexperts.com
15)Converts a string to uppercase letters - strtoupper()
It is s used to convert all alphabetic characters of a string to uppercase.
Example:
<?php
$qa_var='welcome to qawithexperts.com';
echo strtolower($qa_var);
?>
Output: WELCOME TO QAWITHEXPERTS.COM
16)substr() function
It is used to cut a part of a string from a string, starting at a specified position.
<?php
$qa_string="Welcome to qawithexperts.com";
echo $qa_string;
echo '<br>';
echo substr($qa_string,-1,1);
echo '<br>';
?>
Output:
Welcome to qawithexperts.com
17)str_word_count() function
It is used to count the number of words in a string.
<?php
echo str_word_count("Welcome to qawithexperts");
?>
Output: 3
18)str_repeat() function
It is used to repeat a string, a specified number of times.
example:
<?php
echo str_repeat("+",3);
echo('<br>');
echo str_repeat("$$",4);
echo('<br>');
echo str_repeat("%*%",2);
?>
Output:
+++
$$$
%*%%*%