PHP Strings

What is String in PHP

A string in PHP is a sequence of characters that can include letters, numbers, special characters, and even arithmetic values. The simplest way to define a string is by enclosing it in single quotation marks ('), like this:

$my_string = 'Hello World';

You can also use double quotation marks ("). However, there are differences in how single and double quoted strings are treated. Strings enclosed in single quotes are interpreted almost literally, while double quoted strings allow for variable interpolation, where variables are replaced with their actual values, and certain escape sequences are interpreted.

Common escape sequences include:

  • \n for newline
  • \r for carriage return
  • \t for tab
  • \$ for the dollar sign ($)
  • \" for double quote (")
  • \\ for backslash (\)

Here's an example to illustrate the difference:

<?php
$my_str = 'World';
echo "Hello, $my_str!<br>";      // Displays: Hello World!
echo 'Hello, $my_str!<br>';      // Displays: Hello, $my_str!

echo '<pre>Hello\tWorld!</pre>'; // Displays: Hello\tWorld!
echo "<pre>Hello\tWorld!</pre>"; // Displays: Hello   World!
echo 'I\'ll be back';            // Displays: I'll be back
?>

Manipulating PHP Strings

PHP offers a variety of built-in functions designed for manipulating strings, such as determining the length of a string, finding substrings or characters, replacing parts of a string, splitting strings, and more. Below are examples of some of these functions.

Calculating the Length of a String

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

<?php
$my_str = 'Welcome to Tutorial Republic';

// Outputs: 28
echo strlen($my_str);
?>

Counting Number of Words in a String

The str_word_count() function calculates the number of words in a string.

<?php
$my_str = 'The quick brown fox jumps over the lazy dog.';

// Outputs: 9
echo str_word_count($my_str);
?>

Replacing Text within Strings

The str_replace() function substitutes all instances of a specified search string with a new string in the target string.

<?php
$my_str = 'If the facts do not fit the theory, change the facts.';

// Display replaced string
echo str_replace("facts", "truth", $my_str);
?>

The output of the above code will be:

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

You can optionally provide a fourth argument to the str_replace() function to determine the number of replacements made, like this:

<?php
$my_str = 'If the facts do not fit the theory, change the facts.';

// Perform string replacement
str_replace("facts", "truth", $my_str, $count);

// Display number of replacements performed
echo "The text was replaced $count times.";
?>

The result of executing the provided code will display:

The string replacement operation was performed 2 times.

Reversing a String

The strrev() function is used to reverse the sequence of characters in a string.

<?php
$my_str = 'You can do anything, but not everything.';

// Display reversed string
echo strrev($my_str);
?>

The output of the above code will be:

.gnihtyreve ton tub ,gnihtyna od nac uoY

PHP String Reference

For a comprehensive list of helpful string functions, refer to the PHP String Reference.