PHP Strings: what you should know?

Last Updated : May 15, 2021

PHP Strings are considered a sequence of alphanumeric characters like letters, numbers, special characters, or combinations. PHP String must be enclosed with single quotes(‘) or double quotes(“) with necessary escape sequence characters if needed.

How to create a String in PHP?

There are four ways available to create String in PHP, please find below about them in detail,

  • Single Quoted String
  • Double Quoted String
  • Heredoc
  • Nowdoc

Single Quoted String in PHP

To create a string using a single quote, we have to enclose letters with a single quotation. This is one of the quick ways to create a String in PHP and escape characters must be included if we use another single quote inside the statement. Please find below the examples,

<?php
echo 'This is sample text <br>';
echo 'This is sample <br>
test in multiple line';
//echo 'This is sample text has it's own char';
?>

Double Quoted String in PHP

We can create a string using a double quotation and it can render the variables in it. Please find below the examples,

<?php
echo "This is sample text inside double quote <br>";
echo "This is sample <br>
test in multiple line";
//echo "This is sample text has it"s own char";
?>

Heredoc String in PHP

Heredoc method is similar to string with double quotation. The Heredoc string must be started using delimiter <<< and custom identifier, also the string must end with the same identifier along with >>>. Heredoc will acts as double quoted string, so it will parse the variable’s value and expand. Please find below the example,

<?php
$author = "Elzie Crisler Segar";
$text = <<<EOL
Popeye the Sailor is a fictional
cartoon character
created by $author
EOL;
echo '<pre>'. $text .'</pre>';
?>

Nowdoc String in PHP

Nowdoc is the same as Heredoc except its identifier enclosed with single quotes. Also, Nowdoc act as a single quote string statement that doesn’t parse or render the variable’s value inside the string. Please find below the example,

<?php
$author = "Elzie Crisler Segar";
$text = <<<'EOD'
Popeye the Sailor is a fictional
cartoon character
created by $author
EOD;
echo '<pre>'. $text .'</pre>';
?>

Escape Sequence in String

When we use Double quote or Heredoc for creating the string, PHP automatically parses escape sequences into specific special characters. Please find below the list of important escape sequences,

  • \n – Its escape as newline or linefeed
  • \r – Its escape as a carriage return or Enter
  • \t – Its escape as horizontal tab or tab space
  • \$ – Its escape as a dollar sign
  • \\ – Its escape as backslash
  • \’ – Its escape as single quote
  • \” – Its escape as double quote

These escape sequences will help to format the string and parse overall statement effectively.

How to include variables in String?

PHP Variable can be included in the string with the necessary escape string or with curly braces({}). All the variables are parsed inside double-quoted string or Heredoc string. Please find below the examples,

<?php
$name = " John Doe";
$fruit = "Apple";
echo "Hi, My name is $name and I like $fruit <br>";
echo 'Hi, My name is $name and I like $fruit <br>';
echo "Hi, My name is {$name} and I like {$fruit} <br>";
echo 'Hi, My name is {$name} and I like {$fruit} <br>';

$text = <<<EOL
Hi, My name is $name
I like {$fruit}
EOL;
echo '<pre>'. $text .'</pre>';

// more scenario
$name = "PHP";
echo "Welcome to $nameand HTML Tutorial <br>";
echo 'Welcome to $nameand HTML Tutorial';
?>

String Concatenation in PHP

In PHP we can concatenate the string using the dot(.) operator, there are two common methods to concatenate two strings or strings with variables, etc. The first one is . and the second one is .= assignments. Please find below the examples,

<?php
$greeting = "Welcome";
echo $greeting . " to PHP Tutorial";
echo '<br>';

$greeting .= " to PHP Tutorial";
echo $greeting;
echo '<br>';

$a = "Hello";
$b = "World!";
echo $a .' '. $b;
echo '<br>';

$a = "Hello";
$b = "World!";
echo $a ,' ', $b;
?>

Top 10 useful String Functions in PHP

There are a bunch of String functions available in PHP. We will learn about them in detail in the Function reference section. Please find below the most commonly used String functions below,

str_replace – Replace string with another string

The str_replace() function used to replace one string with another string.

<?php
$greeting = "Hello World!";
echo str_replace("World", "Universe!", $greeting);
?>

strpos – Find a character/string in another string

The strpos() function used to find the first occurances of character or string in another string.

<?php
$greeting = "Hello World!";
$pos = strpos($greeting, "World"); //case-sensitive
if($pos) {
	echo 'Yes, it is available.';
} else {
	echo 'No, it is not.';
}
?>

strlen – Returns the length of a string

The strlen() function used to get the length of a string, it will consider spaces inside the string as well.

<?php
$greeting = "Welcome to our PHP Tutorial";
echo strlen($greeting);
?>

str_word_count – Returns detail about the word in a string

This str_word_count() function is used to get information about the words available in a string, by default it will return word’s count of the string.

<?php
$greeting = "Hello World!";
echo str_word_count($greeting);
?>

str_split – Splits the string with a certain length

The str_split() function is used to split the string with a given length and returns output as an array.

<?php
$greeting = "Hello World!";
$splits = str_split($greeting, 5);
var_dump($splits);
print '<pre>'; print_r($splits); print '</pre>';
?>

strtoupper – Convert string into upper case

This strtoupper() function used to convert given string into upper case letters.

<?php
$greeting = "hello world!";
echo strtoupper($greeting);
?>

strtolower – Convert string into lower case

This strtolower() function used to convert given string into lower case letters.

<?php
$greeting = "HELLO WORLD";
echo strtolower($greeting);
?>

ucwords – Convert the first letter of each word into upper case

This ucwords() function used to make the first letter of each word into the upper case in a string.

<?php
$greeting = "welcome to our PHP tutorial";
echo ucwords($greeting);
?>

trim – Remove the white spaces in the string

The trim() function used to remove the white spaces available at the beginning and the end of the string.

<?php
$greeting = " hello world     ";
echo '->'.$greeting .'<-<br>';
echo strlen($greeting). '<br>';

$greeting = trim($greeting);
echo '->'.$greeting .'<-<br>';
echo strlen($greeting). '<br>';
?>

strrev – Reverse the string

The strrev() function used to reverse the string and return as an output.

<?php
$greeting = "Welcome to our PHP Tutorial!";
echo strrev($greeting) .'<br>';

echo 'saippuakivikauppias'. '<br>';
echo strrev('saippuakivikauppias'); // longest known palindrome
?>

In this tutorial we learned about creation and manipulation of String in PHP, we will learn about String functions in detail later in this tutorial series.