PHP Strings

In this lesson, you will learn how to create, store or manipulate strings in PHP. A string is a type that contains letters, numbers, and symbols or a combination of these.

How to create a string in PHP

The simplest way to create a string in PHP is to enclose the string literal in single or double quotes.

Example of creating a string in PHP

$str1 = "Web design";
$str2 = 'Web development';

echo $str1;
echo "<br/>";
echo $str2;

A string can be enclosed with single or double quotes. using a single-quotes make the string to be treated almost literally, whereas using the double-quotes replace variables with their values as well as especially interpreting specific escape sequences.

The escape-sequence replacements are:

\n is replaced by the new line character
\r is replaced by the carriage-return character
\t is replaced by the tab character
\$ is replaced by the dollar sign
\” is replaced by a double-quote
\\ is replaced by a single backslash

Example of creating a string II

  $str = 'John';
  echo "Hello, $str!<br>";      // Displays: Hello John!
  echo 'Hello, $str!<br>';      // Displays: Hello, $str!
  echo '<pre>Hello\tJohn!</pre>'; // Displays: Hello\tJohn!
  echo "<pre>Hello\tJohn!</pre>"; // Displays: Hello   Joh!
  echo 'o\'reilly';            // Displays: I'll O'reilly

As you can see, in case of single quotes the string was displayed as it is but, in case of double quotes, the value of variable replaced the variable in the string.

Example of creating a string III

  $str = 'John';

Heredoc in PHP

Heredoc is used to produce a relatively complex and long string as compared to double quotes. If you have to display a paragraph of text longer than one line, you can use echo statements for each line or heredoc for the whole paragraph at once.

The heredoc is created with <<< followed by a delimiting identifier, followed by text starting on the next line, and then closed by the same identifier on its line. The closing identifier must not be indented.

Example of Heredoc

echo <<<TEXT
Your text with multiple line can go here. This is a test of how to output the text inside a string longer than one line of code.


Your text with multiple line can go here. This is a test of how to output the text inside a string longer than one line of code.

Nowdoc in PHP

The Nowdoc string is similar to the heredoc, but it works like the way string enclosed within single quotes work. No parsing of variables takes place inside the Nowdoc. It is ideal in the situation when you have to use the raw data that do not need to be parsed.

Example of Nowdoc in PHP

echo <<<'TEXT'
Your text with multiple line can go here. This is a test of how to output the text inside a string longer than one line of code.
Tip: Choose a string delimiting identifier that you are sure won’t be repeated in text paragraph you want to display. Like TEXT, EOT, EOL, EOF, etc.

Joining Strings in PHP

To concatenate two strings together, use the dot operator. These strings could be stored in variables as well.

Example of joining strings in PHP

$user_name = "John";
echo "Hello ". $user_name;

PHP Strings Functions

Finding the length of a string

The strlen() function is used to find the total number of characters in a string. The function takes a string as a parameter and returns the length of the given string. It will return zero if the string is empty.

Example of stnlen() function

$user_name = "John";
echo "Length of the string is ". strlen($user_name); 

It will produce the following output
Length of the string is 4

How to count the number of words in a string

Str_word_count() function counts the number of words insides string.

The syntax is

Str_word_count(string $str [, int $format =0 [, string $charlist ]]):mixed

Example of counting number of word in a string

$my_str = "Good moring everyone!";
echo "Number of words: ". str_word_count($my_str);

This code will output
Number of words: 3

Remove all HTML tags

Stripg_tags() function will remove all HTML and PHP tags from a string.

Strip_tags(string $str [,string $allowable_tags]) : string

strip_tags($input, '<br>');

Changing case of string

Ucfirst() method make a string’s first character uppercase.

ucfirst( string $str) : string

Example of changing case of string

$foo = 'hello world!';
$foo = ucfirst($foo);             // Hello world!

strtolower() function will convert a string into lowercase and strtoupper() function will change a string into upper case

Example of strtolower() function


$str = "When you cannot find sunshine, Be sunshine yourself.";
echo strtolower($str);
echo “<br/>”;
echo strtoupper($str);

PHP Echo and Print Tutorial Home PHP Operators


Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers.

View all posts by Tutorials Panel →