Most common PHP string manipulation questions

Most common PHP string manipulation questions

Learn PHP by answering most common PHP string questions

These are the most common PHP string manipulation questions that I had come around when I was getting started with PHP. Most of these easy concepts still confuse students. But let's take few minutes and take them one by one.

How do I combine two strings together?

The first common PHP string manipulation question is "How do I combine two strings?". Yes, it is very common. The reason is confusion between PHP concatenation operators and other languages'. For instance, in JavaScript concatenation is done using the plus sign (+). So, if you experience this confusion, don't fret. Obviously, for a person with basic knowledge and experience, the answer is simple. We use the dot sign (.) operator for concatenation in PHP. Here is a simple snippet.

$var1 = "Learn";
$var2 = "PHP";
$message = $var1 . ' '.$var2;

But, I am sure we can also think of a couple of more ways to answer this common PHP string manipulation question. For instance, we can use string interpolation with curly braces to combine the two string variables.

$var1 = "Learn";
$var2 = "PHP";
$message = "{$var1} {$var2}";

Normally, we don't need curly braces when interpolating strings. But, we need them in this scenario. Since we are performing interpolation and concatenation. Beware that this works with double quotations string only. Finally, we can also use printf and echo to combine strings and print them.

How to check if a string contains another word in PHP?

Next, checking if a string contains another string seems a very important utility that PHP didn't provide until recently (The release of version 8). Yet, until now there were a number of ways of answer this common PHP string manipulation question and we will see them all.

Okay, let's see how we will answer this question one by one. Firstly, the simple & go-to solution is to use strpos() function.

Using strpos() funciton

This method works fine as long as we use identity o This method works fine as long as we use the identity operator instead of equality operators. Because the function, strpos(), returns 0 when the second string is located as the first character or string. And, PHP will cast 0 as FALSE during comparison operators.

$var1 = "Programming with PHP is Awesome.";
$var2 = "PHP";
if(strpos($var1, $var2) !== FALSE) {
   echo "There is $var2 in $var1";
}

Using substr_count() php function

We can use the substr_count() PHP string function to count the number of occurrences of one string in another string. That means we can check if the number of occurrences is greater than 0 and then be able to answer our PHP string manipulation question. Here is an example:-

$var1 = "Programming with PHP is Awesome.";
$var2 = "PHP";
if(substr_count($var1, $var2) > 0) {
   echo "There is $var2 in $var1";
}

There are more ways of do this. Please check my complete blog on common PHP string manipulation questions