PHP String
PHP string is a sequence of characters i.e., used to store and manipulate text. PHP
supports only 256-character set and so that it does not offer native Unicode support.
There are 4 ways to specify a string literal in PHP.
1. single quoted
2. double quoted
3. heredoc syntax
4. newdoc syntax (since PHP 5.3)
Single Quoted
We can create a string in PHP by enclosing the text in a single-quote. It is the
easiest way to specify string 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 same as they specified instead of having any special
meaning.
Example 1:
<?php
$str='Hello text within single quote';
echo $str;
?>
Output: Hello text within single quote
We can store multiple line text, special characters, and escape sequences in a single-quoted
PHP string.
Example 2
<?php
$str1='Hello text
multiple line
text within single quoted string';
$str2='Using double "quote" directly inside single quoted string';
$str3='Using escape sequences \n in single quoted string';
echo "$str1 <br/> $str2 <br/> $str3";
?>
Output:
Hello text multiple line text within single quoted string
Using double "quote" directly inside single quoted string
Using escape sequences \n in single quoted string
Example 3
<?php
$num1=10;
$str1='trying variable $num1';
$str2='trying backslash n and backslash t inside single quoted string \n \t';
$str3='Using single quote \'my quote\' and \\backslash';
echo "$str1 <br/> $str2 <br/> $str3";
?>
Output:
trying variable $num1
trying backslash n and backslash t inside single quoted string \n \t
Using single quote 'my quote' and \backslash
Double Quoted
In PHP, we can specify string through enclosing text within double quote also. But escape
sequences and variables will be interpreted using double quote PHP strings.
Example 1
<?php
$str="Hello text within double quote";
echo $str;
?>
Output: Hello text within double quote
We can't use double quote directly inside double quoted string.
Example 2
<?php
$str1="Using double "quote" directly inside double quoted string";
echo $str1;
?>
Output: Parse error: syntax error, unexpected 'quote' (T_STRING) in
C:\wamp\www\string1.php on line 2
We can store multiple line text, special characters and escape sequences in a
double quoted PHP string.
Example 3
<?php
$str1="Hello text
multiple line
text within double quoted string";
$str2="Using double \"quote\" with backslash inside double quoted string";
$str3="Using escape sequences \n in double quoted string";
echo "$str1 <br/> $str2 <br/> $str3";
?>
Output:
Hello text multiple line text within double quoted string
Using double "quote" with backslash inside double quoted string
Using escape sequences in double quoted string
In double quoted strings, variable will be interpreted.
Example 4
<?php
$num1=10;
echo "Number is: $num1";
?>
Output: Number is: 10
Heredoc
Heredoc syntax (<<<) is the third way to delimit strings. In Heredoc syntax, an
identifier is provided after this heredoc <<< operator, and immediately a new line
is started to write any text. To close the quotation, the string follows itself and
then again that same identifier is provided. That closing identifier must begin from
the new line without any whitespace or tab.
PHP makes it easier to handle multi-line text, interpolate variables and embed
complex expressions without the syntax complications that can arise with other
string definition methods.
Naming Rules
The identifier should follow the naming rule that it must contain only alphanumeric
characters and underscores and must start with an underscore or a non-digit
character.
For Example,
<?php
$str = <<<Demo
It is a simple example
Demo; //Valid code as whitespace or tab is not valid before closing identifier
echo $str;
?>
Output: It is a simple example
Ex2:
<?php
$user=”Srikanth”;
$email=”[email protected]”;
$ details=<<<user_details
User information
Name: $user;
Email: $email;
User_details;
Echo $details;
Output:
User information
Name: Srikanth;
Email: [email protected];
Newdoc
Newdoc is similar to the heredoc, but in newdoc parsing is not done. It is also
identified with three less than symbols <<< followed by an identifier. But here
identifier is enclosed in single-quote, e.g. <<<'EXP'. Newdoc follows the same rule
as heredocs.
The difference between newdoc and heredoc is that - Newdoc is a single-quoted
string whereas heredoc is a double-quoted string.
Ex1:
<?php
$str = <<<'DEMO'
Welcome to javaTpoint.
Learn with newdoc example.
DEMO;
echo $str;
echo '</br>';
echo <<< 'Demo' // Here we are not storing string content in variable str.
Welcome to javaTpoint.
Learn with newdoc example.
Demo;
?>
Output:
Welcome to javaTpoint. Learn with newdoc example.
Welcome to javaTpoint. Learn with newdoc example.
PHP String Functions
PHP provides various string functions to search, format and manipulate textual data
within PHP applications. Using string functions developers can efficiently work with
strings, perform complex operations and enhance the functionalities of their
applications.
A list of PHP string functions are given below.
PHP strtolower() function
The strtolower() function returns string in lowercase letter.
Syntax: string strtolower ( string $string )
Example
<?php
$str="My name is KHAN";
$str=strtolower($str);
echo $str;
?>
Output: my name is khan
PHP strtoupper() function
The strtoupper() function returns string in uppercase letter.
Syntax: string strtoupper ( string $string )
Example
<?php
$str="My name is KHAN";
$str=strtoupper($str);
echo $str;
?>
Output:
MY NAME IS KHAN
PHP strrev() function
The strrev() function returns reversed string.
Syntax: string strrev ( string $string )
Example
<?php
$str="my name is Sonoo jaiswal";
$str=strrev($str);
echo $str;
?>
Output: lawsiaj oonoS si eman ym
PHP strlen() function
The strlen() function returns length of the string.
Syntax: int strlen ( string $string )
Example
<?php
$str="my name is Sonoo jaiswal";
$str=strlen($str);
echo $str;
?>
Output: 24
Str_word_count() – counts the number of words in a string
Ex: echo str_word_count() (“I LIKE PHP”);
Output: 3
strpos()- finds the position of the first occurrence of a substring in
a string.
Ex: echo strpos(“Hello World!”,”World”);
Output: 6 // returns the index of first occurrence.
str_replace()- replaces all occurrence of a search string with a
replacement string.
Ex: echo str_replace(“World”, ”PHP”, ”Hello World”);
Output: Hello PHP //replaces World with PHP
substr()- returns part of a string
Ex: echo substr(“”Hello World”,6,5);
Output: World // extracts 5 characters starting from position 6
trim()- trims whitespaces (or other characters) from the beginning
and end of a string.
Ex: echo trim(“ Hello PHP! “);
Output: Hello PHP!
ucfirst()- converts the character of the string to uppercase
Ex: echo ucfirst(“hello PHP”);
Output: Hello PHP
ucwords()-capitalizes the first character of each word in a string.
Ex: echo ucwords(“hello php!”);
Output: Hello Php!
str_repeat()-repeats a string a specified number of times.
Ex: echo str_repeat(“PHP”,3);
Output: PHPPHPPHP
str_split()- splits a string into an array of characters.
Ex: print_r(str_split(“hello”));
Output: Array([0]=>h,[1]=>e,[2]=>l,[3]=>l,[4]=>o)
Str_pad()-pads a string to certain length with another string
Ex: echo str_pad (“Hello”,10,”*”,STR_PAD_BOTH);
Output: **Hello***
Str_shuffle(): randomly shuffles the characters in a string
Ex: echo str_shuffle(“Hello”);
Output: random output