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:
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
There are four different types of declaration.
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.
Syntax:
<?php echo 'Login to the Page to view this page template'; ?>
Output:
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.
<?php echo 'I \'ll be in 10 minutes just wait here'; ?>
Output:
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.
Syntax:
<?php $myname = 'Pavan Kumar Sake'; echo "$myname and Surendra Gandham is friends since from the College times"; ?>
Output:
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.
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.
Syntax:
<?php echo "I want 100\$ now urgently"; echo "\n This content is appearing after providing linefeed"; ?>
Output:
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.
Syntax:
<?php $my_name = "Pavan Kumar Sake"; echo <<<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 EOT; ?>
Output:
<< Syntax: Output: 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. Syntax: Output: Here we understand the concept 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 Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!<?php
$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
EOT;
echo $eot_variable;
?>
</p>
<p><strong>Output:</strong></p>
<p><img src="https://img.php.cn/upload/article/000/000/000/172490807471311.png" alt="Chaîne en PHP" ></p>
<h4>4. PHP String creation using Nowdoc String creation method – PHP Nowdoc</h4>
<p>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.</p>
<p>Let us have a code example of Nowdoc method below:</p>
<p><strong>Syntax:</strong></p>
<pre class="brush:php;toolbar:false"><?php
$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
EOT;
echo $my_variable;
?>
<?php
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 ");
?>
Conclusion