Introduction to String in PHP
Basically we all know that a string is some collection of Characters. It is also one of the data types actually supported by the PHP programming language. Strings may contain alphanumeric characters. It can be created by the following ways listed:
- You can declare a variable and then you can assign a string to a character
- By using the echo statement
- String/strings are language construct, it can help capture words.
- Other than this learning how strings works in PHP programming language can make you manipulate how to use strings very effectively like a productive developer.
How to declare String in PHP using various methods
There are four different types of declaration.
- By using Single quotes
- By using Double Quotes
- By using the Heredoc string creation method++
- By using Nowdoc string creation method
1. PHP String creation method using Single Quotes
Creating string/strings using the Single Quotes is the easiest and the simplest of creating a string. Now let’s have a look at the below-listed PHP syntax which is creating a simple string.
echo 'Login to the Page to view this page template';
If this PHP Single Quote string is a part of the string value then we make it escape using the character of backslash. Check that code syntax below.
echo 'I \'ll be in 10 minutes just wait here';
2. PHP String creation method using the Double Quotes
Mostly in the String creation in PHP language, Double Quotes will be used. Double Quotes String declaration method can create complex strings rather than the strings created using the single quotes.
We can also use variable names inside the double quotes so that variable values will be displayed as we want. Just have a look at the example of a double-quote declaration, variable declaration so that you will understand the concept of the String Declaration using the Double Quotes.
$myname = 'Pavan Kumar Sake';
echo "$myname and Surendra Gandham is friends since from the College times";
The above example will create a simple string variable “myname” with the value “Pavan Kumar Sake”. This string variable can now be used at any time in the PHP programming language as you want.
4.5 (2,979 ratings)
Double Quotes declared strings can also escape special characters. \n for line feed, \$ for the dollar sign, etc., can be used to get the outputs of empty line and dollar sign. Likewise, there are ways to escape special characters using Double Quotes.
echo "I want 100\$ now urgently";
echo "\n This content is appearing after providing linefeed";
3. PHP String creation using Heredoc String creation method – PHP Heredoc
Here we will know what is Heredoc. The main methodology of Heredoc is to create the most complex strings in PHP when compared with the Double Quotes string declaration.
Heredoc will support all the features of the double-quotes. Along with that Heredoc will also allow you to create the string values without php string concatenation in more than one line. You can create as many lines with strings as you want. Multiple lines with strings can be created using the Heredoc method. One can even escape the Double Quotes inside the Heredoc method.
Let’s have an example of the PHP Heredoc method to create single/multiple string values.
$my_name = "Pavan Kumar Sake";
When $my_name was a small boy,
He worked a lot and gained a lot
of knowledge yet he is simple like
the "small boy " in the childhood
<<<EOT is the string delimiter, EOT means End Of Text. EOT should be used/specified at the beginning of the string and also at the end. You can also use any variable declaration for <<<EOT.
$my_name = "PK Sake";
$eot_variable = <<<EOT
When $my_name was a small boy,
He worked a lot and gained a lot of knowledge yet he is simple like the "small boy " in the childhood
4. PHP String creation using Nowdoc String creation method – PHP Nowdoc
The PHP Nowdoc string creation method is very much similar to the PHP Heredoc string creation method but the Nowdoc method mainly works like how the single quotes work. Parsing will not take place inside the Nowdoc. Nowdoc method is ideal then we are working with the raw data which is not at all needed to be parsed.
Let us have a code example of Nowdoc method below:
$my_name = "Pavan Kumar Sake";
$my_variable = <<<'EOT'
When the big $my_name was a small boy,
He has done many funny things even this generation can’t imagine He is always like a "small boy "since from childhood
Other than string declaration there are some string functions that will be very helpful in developing small web programs to the large projects. Kindly have a look at them. Also, know what is the output difference between the string double quote declaration and the single quote declaration.
strtolower, strtoupper, strlen, explode, substr, str_replace, strops, sha1, md5, str_word_count, ucfirst, lcfirst are the string functions. These string functions are very helpful in creating projects or Programs.
“strtolower” converts all the string characters to the lowercase letters whereas “strtoupper” will convert them to upper case letters. “strlen” will provide the string length(characters count). “explode” function will convert strings into array variables. “substr” will return part of the string by using 3 basic parameters. “str_replace” will replace some string content using 3 basic arguments. “strpos” function will return the position of the specific string. “sha1” function returns hash code of the string. “md5” function provides md5 hash value for the string. Likewise, other strings function too.
echo strtolower("1. PAVAN kumar is a Writer ");
echo strtoupper("2. PAVAN kumar is a Writer \n");
echo "3. ";
echo strlen("PAVAN kumar is a Writer");
echo substr(" 4. PAVAN kumar is a Writer ",0,12);
echo str_replace('PAVAN','VASU'," 5. PAVAN kumar is a Writer \n");
echo "6. ";
echo strpos("PAVAN kumar is a Writer ",'kumar');
echo " 7. ";
echo sha1("PAVAN kumar is a Writer ");
echo "\n8. ";
echo md5("PAVAN kumar is a Writer ");
Here we understand the concept of “String in PHP” with the variety of string declaration methods along with the basic introduction of important string functions which are helping a lot when doing php programs/projects
This has been a guide to String in PHP. Here we discuss the introduction and how to declare String using various methods. You may also have a look at the following articles to learn more –