首页 > 后端开发 > php教程 > PHP 中的字符串

PHP 中的字符串

WBOY
发布: 2024-08-29 13:07:48
原创
403 人浏览过

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';
?>
登录后复制

Output:

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';
?>
登录后复制

Output:

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";
?>
登录后复制

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:

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;
?>
登录后复制

Output:

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="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;
?>
登录后复制

Output:

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  ");
?>
登录后复制

Output:

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

以上是PHP 中的字符串的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板