PHP Strings


In this article, you will learn about PHP strings in detail, how the PHP strings are created and also about some of the built-in PHP functions used to manipulate strings.

php strings

PHP Strings: Introduction
How to create Strings?
PHP escape sequences
PHP string concatenation
PHP strlen()
PHP str_word_count()
PHP strrev()
PHP str_replace()

PHP Strings: Introduction

A string in any programming language is a sequence or stream of characters. There is no limit in the length of a string. Depending on the available memory, you can make strings as long as you wish.

Before you can use strings you have to create it! A string can be used directly in a function or it can be stored in a variable. Here is a basic example where we will create a string twice: first storing it into a variable and in the second case we send the string directly to echo.

<?php
$string = "try to program”;

echo "I love";

echo $string;

?>

In the above example, the first string will be stored into the variable $string, while the second string will be used in the echo and not be stored. Remember to save your strings into variables, if you plan on using them more than once! Here is the output from our example code.

Output:

I love try to program

How to create PHP Strings?

There are two ways of creating strings in PHP.

Single-quote strings: 

This type of strings does not process special characters inside quotes. Thus far we have created strings using double-quotes, but it is just as correct to create a string using single-quotes, otherwise known as apostrophes.

PHP Code:

<?php
$string = ‘try to program’;

echo ‘I love’;

echo $string;
?>

If you want to use a single quote within the string you have to escape the single quote with a backslash \  (like \’) .

PHP Code:

echo 'php - It\'s time.'; // ths will output It's time.

Double-quote strings :

Unlike single-quote strings, double-quoted strings in PHP are capable of processing special characters. We have used double-quotes and will continue to use them as the primary method for forming strings. Double-quotes allow for many special escaped characters to be used that you cannot do with a single-quoted string. 

Before showing examples of double-quoted strings, let’s learn about escape sequence in PHP.

PHP Escape Sequences

In PHP, any character starting with a backslash (\) is treated as an escape sequence and is replaced with special characters. Here are some of the escape sequences in PHP.

  • \n is replaced by a new line
  • \” is replaced by a single double quote i.e “
  • \t is replaced by a tab space
  • \r is replaced by a carriage-return character
  • \$ is replaced by a dollar sign ($)
  • \\ is replaced by a single backslash (\)

Example

$newline = "A newline is \n";

$return = "A carriage return is \r";

$tab = "A tab is \t";

$dollar = "A dollar sign is \$";

$doublequote = "A double-quote is \"";

Note: If you try to escape a character that doesn’t need to be, such as an apostrophe, then the backslash will show up when you output the string.

Example:

<?php

// double-quote strings 

echo "Welcome to try to program \n";

$string  = "try to program";

echo "Welcome to $string";

?>

Output:

Welcome to try to program

Welcome to try to program

PHP treats everything inside double quotes(” “) as Strings. Now let’s learn about the working of the various string functions and how to implement them along with some special properties of strings and also about the concatenation of strings in PHP.

PHP String Concatenation: How to concatenate strings in PHP?

Concatenation in simple terms means merging two or more different strings or characters into one. In PHP, dot(.) operator is used to concatenate strings.

Example

<?php

$string1 = "Hello";
$string2 = " ";
$string3 = "World";

$string4 = $string1.$string2.$string3;
echo $string3;
?>

Output:

Hello World

PHP String: Built-in functions

strlen(): Get The Length of a String

This function is used to find the length of a string.

Example:

<?php

echo strlen("welcome to try to program");

?>

Output:

25

str_word_count(): Count The Number of Words in a String

This function is used to find the total number of words in a PHP string.

Example:

<?php
 echo str_word_count("try to program"); 
?>

Output:

3

strrev(): Reverse a String

This function is used to reverse a string in PHP. 

Example:

<?php

echo strrev("try to program");

?>

Output:

Margorp ot yrt

str_replace(): Replace Text Within a String

This function replaces some characters with some other characters in a string.

Example:

<?php
echo str_replace("learn", "program", "try to learn"); 
?>

Output:

Try to program