PHP string is a sequence of characters i.e., used to store and manipulate text. PHP supports only 256-character sets and so it does not offer native Unicode support. There are 4 ways to specify a string literal in PHP.
We can create a string in PHP by enclosing the text in a single-quote. It is the easiest way to specify strings in PHP.
For specifying a literal single quote, escape it with a backslash (\) and to specify a literal backslash (\) use double backslash (\\). All the other instances with backslash such as \r or \n, will be output the same as they specified instead of having any special meaning.
Example
<?php
$str=’Hello text within single quote’;
echo $str;
?>
Output
Hello text within single quote
String Function Examples
strtolower() function
The strtolower() function returns a string in lowercase letters.
Syntax
string strtolower ( string $string )
Example
<?php
$str=”My name is Parmeshwar”;
$str=strtolower($str);
echo $str;
?>
Output:
my name is parmeshwar
strtoupper() function
The strtoupper() function returns a string in uppercase letter.
Syntax
string strtoupper ( string $string )
Example
<?php
$str=”My name is Parmeshwar”;
$str=strtoupper($str);
echo $str;
?>
Output:
MY NAME IS PARMESWAR
ucfirst() function
The ucfirst() function returns a string converting first character into uppercase. It doesn’t change the case of other characters.
Syntax
string ucfirst ( string $str )
Example
<?php
$str=”my name is PARMESWAR”;
$str=ucfirst($str);
echo $str;
?>
Output:
My name is PARMESWAR
lcfirst() function
The lcfirst() function returns a string converting the first character into lowercase. It doesn’t change the case of other characters.
Syntax
string lcfirst ( string $str )
Example
<?php
$str=”MY name IS Parmeshwar”;
$str=lcfirst($str);
echo $str;
?>
Output:
mY name IS Parmeshwar
ucwords() function
The ucwords() function returns a string converting the first character of each word into uppercase.
Syntax
string ucwords ( string $str )
Example
<?php
$str=”my name is Parmeshwar kuril”;
$str=ucwords($str);
echo $str;
?>
Output:
My Name Is Parmeshwar Kuril