PHP Strings
PHP Strings are one of the most fundamental data types. It is used to handle text data, manipulate user inputs, generate dynamic content, and much more. PHP provides a wide array of functions and techniques for working with strings, making it easy to manipulate and display text.
A string is a sequence of characters used to store and manipulate text data. PHP strings can include letters, numbers, symbols, and special characters. Strings are a versatile data type, commonly used for handling input/output, generating dynamic content, and more.
Declaration of String in PHP
Strings in PHP can be declared using single quotes, double quotes, heredoc, and nowdoc syntax.
1. Single Quotes
Single quotes are used to define simple strings in PHP. The text within single quotes is treated literally, meaning special characters and variables are not interpreted.
<?php
// Singlequote strings
$site = 'Welcome to GeeksforGeeks';
echo $site;
?>
Output
Welcome to GeeksforGeeks
The above program compiles correctly. We have created a string ‘Welcome to GeeksforGeeks’ and stored it in variable and printing it using echo statement.
Let us now look at the below program:
<?php
// Single Quote Strings
$site = 'GeeksforGeeks';
echo 'Welcome to $site';
?>
Output
Welcome to $site
In the above program the echo statement prints the variable name rather than printing the contents of the variables. This is because single-quotes strings in PHP do not process special characters. Hence, the string is unable to identify the ‘$’ sign as the start of a variable name.
2. Double Quotes
Unlike single-quote strings, double-quote strings in PHP are capable of processing special characters.
<?php
// Double Quote Strings
echo "Welcome to GeeksforGeeks \n";
$site = "GeeksforGeeks";
echo "Welcome to $site";
?>
Output
Welcome to GeeksforGeeks Welcome to GeeksforGeeks
In the above program, we can see that the double-quote strings are processing the special characters according to their properties. The ‘\n’ character is not printed and is considered as a new line. Also instead of the variable name $site, “GeeksforGeeks” is printed.
Note: PHP treats everything inside double quotes(” “) as Strings.
In this article, we will learn about the working of the various string functions and how to implement them along with some special properties of strings. Unlike other data types like integers, doubles, etc. Strings do not have any fixed limits or ranges. It can extend to any length as long as it is within the quotes.
It has been discussed earlier that string with single and double quotes are treated differently. Strings within a single quote ignore the special characters, but double-quoted strings recognize the special characters and treat them differently.
<?php
$name = "Krishna";
echo "The name of the geek is $name \n";
echo 'The name of the geek is $name';
?>
Output
The name of the geek is Krishna The name of the geek is $name
Some important and frequently used special characters that are used with double-quoted strings are explained below:
The character begins with a backslash(“\”) is treated as escape sequences and is replaced with special characters. Here are few important escape sequences.
- “\n” is replaced by a new line
- “\t” is replaced by a tab space
- “\$” is replaced by a dollar sign
- “\r” is replaced by a carriage return
- “\\” is replaced by a backslash
- “\”” is replaced by a double quote
- “\'” is replaced by a single quote
The string starting with a dollar sign(“$”) are treated as variables and are replaced with the content of the variables.
3. Heredoc Syntax
The syntax of Heredoc (<<<) is another way to delimit PHP strings. An identifier is given after the heredoc (<<< ) operator, after which any text can be written as a new line is started. To close the syntax, the same identifier is given without any tab or space.
Note: Heredoc syntax is similar to the double-quoted string, without the quotes.
<?php
$input = <<<testHeredoc
Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
I am enjoying this.
testHeredoc;
echo $input;
?>
Output
Welcome to GeeksforGeeks. Started content writing in GeeksforGeeks!. I am enjoying this.
4. Nowdoc Syntax
Nowdoc is very much similar to the heredoc other than the parsing done in heredoc. The syntax is similar to the heredoc syntax with symbol <<< followed by an identifier enclosed in single-quote. The rule for nowdoc is the same as heredoc.
Note: Nowdoc syntax is similar to the single-quoted string.
<?php
$input = <<<'testNowdoc'
Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
testNowdoc;
echo $input;
// Directly printing string
// without any variable
echo <<<'Nowdoc'
Welcome to GFG .
Learning PHP is fun in GFG.
Nowdoc;
?>
Output
Welcome to GeeksforGeeks. Started content writing in GeeksforGeeks!. Welcome to GFG . Learning PHP is fun in GFG.
Some Important Built-in String Functions
PHP built-in functions are some existing library functions that can be used directly in our programs making an appropriate call to them. Below are some important built-in string functions that we use in our daily and regular programs:
1. PHP strlen() Function
This function is used to find the length of a string. This function accepts the string as an argument and returns the length or number of characters in the string.
<?php
echo strlen("Hello GeeksforGeeks!");
?>
Output
20
2. PHP strrev() Function
This function is used to reverse a string. This function accepts a string as an argument and returns its reversed string.
<?php
echo strrev("Hello GeeksforGeeks!");
?>
Output
!skeeGrofskeeG olleH
3. PHP str_replace() Function
This function takes three strings as arguments. The third argument is the original string and the first argument is replaced by the second one. In other words, we can say that it replaces all occurrences of the first argument in the original string with the second argument.
<?php
echo str_replace("Geeks", "World", "Hello GeeksforGeeks!"), "\n";
echo str_replace("for", "World", "Hello GeeksforGeeks!"), "\n";
?>
Output
Hello WorldforWorld! Hello GeeksWorldGeeks!
In the first example, we can see that all occurrences of the word “Geeks” are replaced by “World” in “Hello GeeksforGeeks!”.
4. PHP strpos() Function
This function takes two string arguments and if the second string is present in the first one, it will return the starting position of the string otherwise returns FALSE.
<?php
echo strpos("Hello GeeksforGeeks!", "Geeks"), "\n";
echo strpos("Hello GeeksforGeeks!", "for"), "\n";
var_dump(strpos("Hello GeeksforGeeks!", "Peek"));
?>
Output
6 11 bool(false)
We can see in the above program, in the third example the string “Peek” is not present in the first string, hence this function returns a boolean value false indicating that string is not present.
5. PHP trim() Function
This function allows us to remove whitespaces or strings from both sides of a string.
<?php
echo trim("Hello World!", "Hed!");
?>
Output
llo Worl
6. PHP explode() Function
This function converts a string into an array.
<?php
$input = "Welcome to geeksforgeeks";
print_r(explode(" ",$input));
?>
Output
Array ( [0] => Welcome [1] => to [2] => geeksforgeeks )
7. PHP strtolower() Function
This function converts a string into the lowercase string.
<?php
$input = "WELCOME TO GEEKSFORGEEKS";
echo strtolower($input);
?>
Output
welcome to geeksforgeeks
8. PHP strtoupper() Function
This function converts a string into the uppercase string.
<?php
$input = "Welcome to geeksforgeeks";
echo strtoupper($input);
?>
Output
WELCOME TO GEEKSFORGEEKS
9. PHP strwordcount() Function
This function counts total words in a string.
<?php
$input = "Welcome to GeeksforGeeks";
echo str_word_count($input);
?>
Output
3
10. PHP substr() Function
This function gives the substring of a given string from a given index.
<?php
$input = "Welcome to geeksforgeeks";
echo(substr($input,3));
?>
Output
come to geeksforgeeks