Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

PHP String

PHP string is a sequence of characters i.e., used to store and manipulate text.

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.

For Example

Example 1

<?php
$str1='Hello text
multiple line
text within single quoted string';
$str2=’$str1 morning’
echo $str2
?>

Output:

Hello text multiple line text within single quoted string


$str morning
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
$str1=”Hello text
multiple line
text within single quoted string”;
$str2=”$str1,. morning”
echo $str2
?>

Output:

Hello text multiple line text within single quoted string


Hello text multiple line text within single quoted string,morning

In double quoted strings, variable will be interpreted.

Example 2

<?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.
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

Valid Example

<?php
$str = <<<Demo
It is a valid example
Demo; //Valid code as whitespace or tab is not valid before closing identifi
er
echo $str;
?>

Output:

It is a valid example
ADVERTISEMENT

Heredoc is similar to the double-quoted string, without the double quote,


means that quote in a heredoc are not required. It can also print the variable's
value.

Example

<?php
$city = 'Delhi';
$str = <<<DEMO
Hello! My name is Misthi, and I live in $city.
DEMO;
echo $str;
?>

Output:

ADVERTISEMENT
Hello! My name is Misthi, and I live in Delhi.

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.

Note: Newdoc works as single quotes.

Example-1:

<?php
$str = <<<'DEMO'
Welcome to php
Learn with newdoc example.
DEMO;
echo $str;
echo '</br>';

echo <<< 'Demo' // Here we are not storing string content in variable st
r.
Welcome to php.
Learn with newdoc example.
Demo;
?>

Output:

Welcome to php. Learn with newdoc example.


Welcome to php. Learn with newdoc example.
PHP String Functions

strlen($str)
This function returns the length of the string or the number of characters in
the string including whitespaces.
<?php

$str = "Welcome to php";

// using strlen in echo method


echo "Length of the string is: ". strlen($str);

?>
Copy

Length of the string is: 14

str_word_count($str)
This function returns the number of words in the string. This function comes in
handly in form field validation for some simple validations.
<?php

$str = "Welcome to php";

// using str_word_count in echo method


echo "Number of words in the string are: ". str_word_count($str);

?>

Number of words in the string are: 3


strrev($str)
This function is used to reverse a string.
Let's take an example and see,
<?php

$str = "Welcome to php";

// using strrev in echo method


echo "Reverse: ". strrev($str);

?>
Copy

Reverse: php ot emocleW

strpos($str, $text)
This function is used to find the position of any text/word in a given string. Just
like an array, string also assign index value to the characters stored in it,
starting from zero.
<?php

$str = "Welcome to php";

// using strpos in echo method


echo "Position of 'php' in string: ". strpos($str, 'php');

?>
Copy

Position of 'php' in string: 11


str_replace($replacethis, $replacewith, $str)

This function is used to replace a part of the string with some text. While using
this function, the first argument is the part of string that you want to replace,
second argument is the new text you want to include, and the last argument is
the string variable itself.
Let's take an example,
<?php

$str = str_replace("php", "python", "Welcome to php");

echo $str;

?>
Copy

Welcome to python

ucwords($str)
This function is used for formatting the string. This function converts first
letter/character of every word in the string to uppercase.
Let's take an example and see,
<?php

$str = "welcome to php";

echo ucwords($str);

?>

Welcome To Php
strtoupper($str)
To convert every letter/character of every word of the string to uppercase, one
can use strtoupper() method.
<?php

$str = "welcome to php";

echo strtoupper($str);

?>
Copy

WELCOME TO PHP

strtolower($str)
This function is used to convert every letter/character of a string to lowercase.
<?php

$str = "WELCOME TO PHP";

echo strtolower($str);

?>

welcome to php

substr($str, $start, $length)


This function is used to take out a part of the string(substring), starting from a
particular position, of a particular length.
The first argument is the string itself, second argument is the starting index of
the substring to be exracted and the third argument is the length of the
substring to be extracted.
<?php

$str = "Welcome to php";

echo substr($str, 11, 3);

?>

php

trim($str, charlist)
This function is used to remove extra whitespaces from beginning and the end
of a string. The second argument charlist is optional. We can provide a list of
character, just like a string, as the second argument, to trim/remove those
characters from the main string.
<?php

$str1 = " Hello World ";

echo trim($str1) . "<br/>";

$str2 = "Hello Hello";

echo trim($str2,"Heo");

?>

Hello World

llo Hell

You might also like