Chaîne en PHP

WBOY
Libérer: 2024-08-29 13:07:48
original
312 Les gens l'ont consulté

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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 it 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.

Syntax:

<?php
echo 'Login to the Page to view this page template';
?>
Copier après la connexion

Output:

Chaîne en PHP

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';
?>
Copier après la connexion

Output:

Chaîne en PHP

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.

Syntax:

<?php
$myname = 'Pavan Kumar Sake';
echo "$myname and Surendra Gandham is friends since from the College times";
?>
Copier après la connexion

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";
?>
Copier après la connexion

Output:

Chaîne en PHP

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.

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;
?>
Copier après la connexion

Output:

Chaîne en PHP

<<

Syntax:

<?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;
?>
Copier après la connexion

Output:

Chaîne en PHP

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:

<?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  ");
?>
Copier après la connexion

Output:

Chaîne en PHP

Conclusion

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!

Étiquettes associées:
php
source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!