English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP Strings

In this tutorial, you will learn how to store and manipulate strings in PHP.

What is a string in PHP

A string is a sequence of characters, numbers, special characters, and arithmetic values or combinations of all elements. The simplest way to create a string is to enclose the string literal (i.e., the string characters) in single quotes (') as follows:

$my_string ='World你好';

You can also use double quotes (“). However, single quotes and double quotes have different effects. Strings enclosed in single quotes are processed almost literally, while strings enclosed in double quotes replace variables with their string representation and specifically interpret certain escape sequences.

Escape sequences are replaced with:

  • \n is replaced by a newline

  • \r is replaced by a carriage return

  • \t is replaced by a tab

  • \$ is replaced by the dollar sign itself ($)

  • \" is replaced by a double quote (")

  • \\ is replaced by a single backslash (\)

This is an example to illustrate the difference between single-quoted and double-quoted strings:

<?php
$my_str = 'World';
echo "Hello, $my_str!<br>";      // Display: Hello World!
echo 'Hello, $my_str!<br>';      // Display: Hello, $my_str!
 
echo '<pre>Hello\tWorld!</pre>'; // Display: Hello\tWorld!
echo "<pre>Hello\tWorld!</pre>"; // Display: Hello World!
echo 'I\'ll be back';            // Display: I'll be back
?>
Test and see‹/›

PHP string operations

PHP provides many built-in functions for string handling, such as calculating the length of a string, finding a substring or character, replacing a part of the string with different characters, splitting strings, etc. The following are examples of some of these functions.

strlen() - Calculate the length of the string

The strlen() function is used to calculate the number of characters in a string, including spaces within the string.

<?php
$my_str = 'Welcome to www.oldtoolbag.com';
 
// Output: 28
echo strlen($my_str);
?>
Test and see‹/›

str_word_count() - Calculate the number of words in the string

<?php
The quick brown fox jumps over the lazy dog.
 
// Output: 9
echo str_word_count($my_str);
?>
Test and see‹/›

str_replace() - Replace text in the string

The str_replace() function replaces all occurrences of the search text in the target string.

<?php
$my_str = '如果事实与理论不符,就改变事实.';
 
//Display the replaced string
echo str_replace("事实", "真相", $my_str);
?>
Test and see‹/›

The output of the above code will be:

If the truth does not fit the theory, change the truth.

You can choose to pass the fourth parameter to the str_place() function to determine how many times the string has been replaced, as shown below.

<?php
$my_str = 'If the facts do not fit the theory, change the facts.';
 
// Execute string replacement
str_replace("facts", "truth", $my_str, $count);
 
// Display the number of replacements executed
echo "The text has been replaced $count times.";
?>
Test and see‹/›

The output of the above code will be:

The text has been replaced2time.

strrev() - Reverse string

The strrev() function reverses a string.

<?php
$my_str = 'You can do anything, but not everything.';
 
//Display the reversed string
echo strrev($my_str);
?>
Test and see‹/›

The output of the above code will be:

gnihtyreve ton tub ,gnihtyna od uoY

PHP String Reference

For a complete list of useful string functions, please seePHP String Reference.