String Manipulation

Single Quoted String

1
2
$str="PHP";
echo 'Welcome to learn $str';

Output

1
Welcome to learn $str

Because single quoted treat is as a string as a whole.

Double Quoted String

1
2
3
4
<?php
$str="PHP";
echo "Welcome to learn $str";
?>

Output

1
Welcome to learn PHP

Double quotation has the ability to detect the keyword(variable).

strtoupper(argument)

Change to uppercase

1
2
3
4
5
<?php
$str="hello people";
$converted_str=strtoupper($str);
echo $converted_str;
?>

Output

1
HELLO PEOPLE

strtolower(argument)

Change to lowercase

1
2
3
4
5
6
7
<?php
$str="hello people";
$converted_str=strtoupper($str);
echo "uppercase=".$converted_str."<br>";
$converted_again_str=strtolower($str);
echo "lowercase=".$converted_again_str;
?>

Output

1
2
uppercase=HELLO PEOPLE
lowercase=hello people

ucfirst(argument)

Change first letter to uppercase

1
2
3
4
5
<?php
$str="hello people";
$converted_str=ucfirst($str);
echo $converted_str."<br>";
?>

Output

1
Hello people

lcfirst(argument)

Change first letter to upper case for each words

1
2
3
4
5
<?php
$str="HELLO PEOPLE";
$converted_str=lcfirst($str);
echo $converted_str."<br>";
?>

Output

1
hELLO PEOPLE

strcmp(argument)

To compare string in terms of number of characters

1
2
3
4
5
6
<?php
$str1="HELLO PEOPLE";
$str2="HELLO";
$comparing=strcmp($str1,$str2);
echo $comparing;
?>

Output

1
7

HELLO had to add more required same letters to right in order to be the same

1
2
3
4
5
6
<?php
$str1="HEL";
$str2="HELLO";
$comparing=strcmp($str1,$str2);
echo $comparing;
?>

Output

1
-2

strlen(argument)

To display the number of characters

1
2
3
4
5
<?php
$str1="HELLO PEOPLE";
$length=strlen($str1);
echo $length;
?>

Output

1
12

This included spacing,

substr($str,pos)

Cut characters of string

1
2
3
4
5
<?php
$str1="HELLO PEOPLE";
$cut=substr($str1,7);
echo $cut;
?>

Output

1
EOPLE

The first 7 letters is cut

trim($str,”abc”)

To cut characters by specify the characters

1
2
3
4
5
<?php
$str1="HELLO PEOPLE";
$trim=trim($str1,"HEL");
echo $trim;
?>

Output

1
O PEOP

The HEL is being cut from the string