Strings are an integral part of the programming world. They allow our code to communicate with others. PHP provides various string methods to manipulate strings effortlessly. In this unit, we will learn about string data in PHP.
Index
What Is String?
PHP programming supports string datatype. A string is a sequence of numbers, letters, special characters, and arithmetic values or a combination of all. Everything inside a single quote (‘ ‘) and Double quotes (” “) treats as a string. In simple words, a string is a series of characters used to store and manipulate text.
There are four ways to specify the string literals in PHP:
- Create string using single quotes
- Create string using double quotes
- Heredoc syntax
- Nowdoc syntax
Read Also: PHP Operators
Create string using single quotes
We can create the PHP string by writing the text within single quotes. It is the easiest way to create a string. This type of string does not process the unique characters inside the single quotes.
Example 1
<?php // single-quote strings $a= 'Welcome Suman'; echo $a; ?>
Output
Welcome Suman
In the above example, we create a string ‘Welcome Suman’ and store it in a variable and print it using an echo statement.
Example 2
<?php // single-quote strings $a= 'errorsea.com'; echo 'Welcome to $a'; ?>
Output
Welcome to $a
In the above example, the echo statement prints the variable name rather than printing the variable’s content because the single quote in a string does not process the individual characters. The string is unable to identify the $ sign.
Create string using Double quotes
We can create the PHP string by writing the text within double-quotes. This type of string processes the individual characters within double-quotes. It is used to create relatively complex strings as compared to single quotes.
Example
<?php // double-quote strings $a = "errorsea.com"; echo "Welcome to $a"; ?>
Output
Welcome to errorsea.com
In the above example, as we can see, the double quotes process the special characters. It prints the content of the variable instead of $a.
Some of the special characters that uses with double quotes:
- \n -> a new line
- \t -> a tab space
- \$ -> a dollar sign
- \r -> a carriage return
- \\ -> a backslash
- \” -> a double quote
- \’ -> a single quote
Heredoc Syntax
The heredoc syntax uses to create a complex string compare to double-quotes. If we create a string with multiple lines using double quotes, it will generate an error as heredoc overcomes all the problems we face with double-quotes. Heredoc supports all the features and allows creating of string values with multiple lines. In heredoc syntax, an identifier provides after heredoc <<< operator, and after that, a new line is started to write any text. The identifier should only contain alphanumeric characters and underscores and must begin with an underscore or a non-digit character.
Example 1
<?php $str = <<<ERRORSEA It is a valid example ERRORSEA; //Valid code as whitespace or tab is not valid before closing identifier echo $str; ?>
Output
It is a valid example.
Example 2
<?php $str = <<<ERRORSEA It is Invalid example ERRORSEA; //Invalid code as whitespace or tab is not valid before closing identifier echo $str; ?>
The code in the above example will generate an error. We cannot use whitespace or tab before and after the identifier & semicolon.
Nowdoc syntax
The nowdoc creation method is like the heredoc syntax, but it uses single quotes. No parsing takes place inside the new doc. It can also identify with three less than symbol <<< followed by an identifier.
The difference between the heredoc and the new doc is that it uses single quote strings, and the new doc uses double quote strings.
Example
<?php $str = <<<'ERRORSEA' Welcome Suman. ERRORSEA; echo $str; echo '</br>'; // Here we are not storing string content in variable str. echo <<<'errorsea' Welcome Suman. errorsea; ?>
Output
Welcome Suman. Welcome Suman.
Built-In String Functions
PHP provides many built-in string functions. String values manipulate using PHP string functions. Below we discuss some PHP string functions that we use in our daily and regular programs.
strlen() Function
The strlen() function displays the length of any string. This function accepts the arguments and returns the length or number of characters in the string.
Syntax
strlen(string);
Example
<?php echo strlen("Hello Suman!"); ?>
Output
12
strrev() Function
The string is reverse using strrev() function. This function accepts the arguments and the reverse string.
Syntax
strrev(string);
Example
<?php echo strrev("Hello suman!"); ?>
Output
!namus olleH
str_replace Function
For replacing specific text within a string str_replace(), built function comes in the picture.
Syntax
Str_replace(string to be replaced,text,string);
Example
<?php echo str_replace("Home", "Suman", "Welcome Home"); ?>
Output
Welcome Suman
str_pos 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.
Syntax
Strpos(string,text);
Example
<?php echo strpos("Hello suman!", "suman") ?>
Output
6
strtoupper() Function
It converts the whole string into uppercase.
Syntax
Strtoupper(string);
Example
<?php echo strtoupper("Welcome Suman"); ?>
Output
WELCOME SUMAN
Strtolower() Function
It converts the whole string into lowercase.
Syntax
Strtolower(string);
Example
<?php echo strtolower("WELCOME SUMAN"); ?>
Output
welcome suman
str_word_count() Function
str_word_count function displays the number of words in a specific string.
Syntax
Str_word_count(string);
Example
<?php echo str_word_count("Welcome Suman"); ?>
Output
2
str_repeat() Function
For repeating a string a specific number of times, the str_repeat() function is used.
Syntax
str_repeat(string,repeat);
Example
<?php echo str_repeat("=",13); ?>
Output
=============
strcmp() Function
Using this built-in function, we can compare the two strings. It returns output greater than zero, less than zero, or equal to zero. Returns greater than zero if string 1 is greater than string 2. It returns less than zero If string 1 is less than string 2. If strings are equal, it returns zero.
Syntax
Strcmp(string1,string2);
Example
<?php echo strcmp("Suman","SUMAN"); echo "<br>"; echo strcmp("suman","suman"); //Both the strings are equal echo "<br>"; echo strcmp("Suman","Hello"); echo "<br>"; echo strcmp("a","b"); //compares alphabetically echo "<br>"; echo strcmp("abb baa","abb baa caa"); //compares both strings and returns the result in terms of number of characters. ?>
Output
8192 0 11 -1 -4
substr() Function
Using substr() function, we can display or extract a string from a particular position.
Syntax
substr(string,start,length);
Example
<?php echo substr ("Hello World",6); ?>
Output
World
trim() Function
This function allows us to remove whitespace or predefined characters from both sides of the string.
Syntax
trim(string,charlist);
Example
<?php echo trim("Hello World!", "Hed!"); ?>
Output
llo worl
Read More: PHP Numbers
Conclusion
A string is a set of characters. Everything inside a single quote (‘ ‘) and double quotes (” “) is treated as a string.
In this unit, we learned about the most common use of PHP String functions and strings. We hope you have gained a better understanding of PHP strings. Do check out the other blogs under the PHP section to strengthen your concepts.