A string is a series of characters, such as "Hello world!"
Strings
Strings in PHP can be encapsulated within either double quotation marks (" ") or single quotation marks (' ').
<?php
echo "Hello";
echo "<br>";
echo 'Hello';
?>
/*
Output:
Hello
Hello
*/
Note: There is a significant distinction between double quotes and single quotes in PHP. Double quotes process special characters, while single quotes do not.
Double or Single Quotes?
You can utilize either double or single quotes, but it's important to understand their distinctions.
Double quoted strings interpret special characters, such as variables within the string, returning their respective values.
<?php
$x = "Honey";
echo "Hello $x";
?>
/*
Output:
Hello Honey
*/
Single quoted strings, on the other hand, do not interpret special characters or variables within the string. Instead, they return the string exactly as it was written, including the variable name itself.
<?php
$x = "Honey";
echo 'Hello $x';
?>
/*
Output:
Hello $x
*/
String manipulation
PHP provides a collection of built-in functions that allow you to manipulate strings.
String Length
The strlen()
function in PHP returns the length
of a string.
<?php
echo strlen("Wellcome to WebmasterMaze");
?>
/*
Output: 25
*/
Word Count
The str_word_count()
function in PHP calculates the number of words in a string.
<?php
echo str_word_count("Wellcome to WebmasterMaze");
?>
/*
Output: 3
*/
Search For Text Within a String
The PHP strpos()
function searches for a specified text
within a string. If a match is found, it returns the character position of the first match. If no match is found, it returns FALSE
.
<?php
echo strpos("Wellcome to WebmasterMaze", "WebmasterMaze");
?>
/*
Output: 12
*/
Upper Case
The strtoupper()
function in PHP converts a string to uppercase
.
<?php
$message = "Wellcome to WebmasterMaze";
echo strtoupper($message);
?>
/*
Output:
WELLCOME TO WEBMASTERMAZE
*/
Lower Case
The strtolower()
function in PHP converts a string to lowercase
.
<?php
$greeting = "Wellcome to WebmasterMaze";
echo strtolower($greeting);
?>
/*
Output:
wellcome to webmastermaze
*/
Replace String
The str_replace()
function in PHP substitutes certain characters with others within a string.
Replace the text "WebmasterMaze" with "Google".
<?php
$text = "Wellcome to WebmasterMaze";
echo str_replace("WebmasterMaze", "Google", $text);
?>
/*
Output:
Wellcome to Google
*/
Reverse a String
The strrev()
function in PHP reverses
a string.
Reverse the string "Wellcome to WebmasterMaze":
<?php
$phrase = "Wellcome to WebmasterMaze";
echo strrev($phrase);
?>
/*
Output:
ezaMretsambeW ot emoclleW
*/
Remove Whitespace
Whitespace refers to the space preceding and/or following the actual text, which is often desirable to remove.
The trim()
function eliminates any whitespace from the beginning or end of a string.
<?php
$sentence = " Wellcome to WebmasterMaze ";
echo trim($sentence);
?>
/*
Output:
Wellcome to WebmasterMaze
*/
Convert String into Array
The PHP explode()
function divides a string into an array
. Its first parameter determines where the string will be split.
For example, if you want to split the string into an array of words, use the space character as separator:
<?php
$text = "Welcome to WebmasterMaze";
$words = explode(" ", $text);
// Use print_r() to display the result:
print_r($words);
?>
/*
Output:
Array ( [0] => Welcome [1] => to [2] => WebmasterMaze )
*/
String Concatenation
To combine or concatenate two strings in PHP, you can utilize the "." operator.
<?php
$firstName = "Webmaster";
$lastName = "Maze";
$fullName = $firstName . $lastName;
echo $fullName;
?>
/*
Output:
WebmasterMaze
*/
In the above example, the resulting string is "WebmasterMaze" without a space between the words. To include a space character, you can do this:
<?php
$firstName = "Webmaster";
$lastName = "Maze";
$fullName = $firstName . " " . $lastName;
echo $fullName;
?>
/*
Output:
Webmaster Maze
*/
A simpler and more effective approach involves leveraging double quotes. By enclosing the two variables within double quotes with a space between them, the resultant string will include the white space as well:
<?php
$firstName = "Webmaster";
$lastName = "Maze";
$fullName = "$firstName $lastName";
echo $fullName;
?>
/*
Output:
Webmaster Maze
*/
Slicing
You can retrieve a range of characters using the substr()
function. Simply indicate the starting index and the desired number of characters to return.
Start the slice at index 12 and end the slice 13 positions later:
<?php
$phrase = "Wellcome to WebmasterMaze";
echo substr($phrase, 12, 13);
?>
/*
Output:
WebmasterMaze
*/
Note: Please note that the index of the first character is 0.
Slice to the End
Omitting the length parameter will extend the range to the end of the string.
Start the slice at index 12 and go all the way to the end:
<?php
$message = "Wellcome to WebmasterMaze";
echo substr($message, 12);
?>
/*
Output:
WebmasterMaze
*/
Slice From the End
Negative indexes can be used to initiate the slice from the end of the string.
For example, get the 9 characters, starting from the "W" in WebmasterMaze.
<?php
$text = "Wellcome to WebmasterMaze";
echo substr($text, -13, 9);
?>
/*
Output:
Webmaster
*/
Note: The last character has index -1.
PHP - Escape Characters
To include characters that are otherwise not allowed in a string, use an escape character.
An escape character is a backslash \
followed by the character you want to include.
For example, if you want to insert a double quote inside a string that is enclosed in double quotes, you can use the escape character (\
).
<?php
$description = "This is a \"text line\" with double quotes.";
echo $description;
?>
/*
Output:
This is a "text line" with double quotes.
*/
Alternatively, you can use single quotes to enclose the whole string.
<?php
$description = 'This is a "text line" with double quotes.';
echo $description;
?>
/*
Output:
This is a "text line" with double quotes.
*/
Escape Characters
Other escape sequences used in PHP include:
\'
Single Quote \"
Double Quote \$
PHP variables \n
New Line \r
Carriage Return \t
Tab \f
Form Feed \ooo
Octal value \xhh
Hex value