Home >Backend Development >PHP Problem >There are several ways to declare string variables in php

There are several ways to declare string variables in php

青灯夜游
青灯夜游Original
2022-01-27 16:35:485279browse

There are three ways to declare string variables in php: 1. Use the "$ variable name = 'string content';" statement; 2. Use the "$ variable name = 'string content';" statement; 3. Use delimiters and use the statement "$variable name =

There are several ways to declare string variables in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php declaration string Variable

##1. Use double quotes

Syntax:

$变量名="字符串内容";

Example:

<?php
$str="hello";
var_dump($str);
?>

There are several ways to declare string variables in php

2. Use single quotes

Syntax:

$变量名=&#39;字符串内容&#39;;

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str=&#39;hi&#39;;
var_dump($str);
?>

There are several ways to declare string variables in php

Single quotes are the most efficient, but single quotes do not parse the contained variables.

$a=&#39;aaa&#39;;
$b=&#39;$abbb&#39;;
echo $b;

There are several ways to declare string variables in php

3. Use delimiters

The delimiters in PHP can define a longer string, and The things inside it can be output as is, including newlines, indentation and other formats. Any special characters in the delimiter do not need to be escaped, and the variables in the delimiter can also be parsed. This is one of the reasons why PHP introduces delimiters.

The syntax format of the delimiter is as follows:

$变量名=<<<str
    一段文本
str;

Among them, the symbol

is a keyword and must be used; str The identifiers customized for us are used as the starting identifier and ending identifier of the text. The names of the preceding and following identifiers must be exactly the same.

In addition, the semicolon after the end identifier cannot be omitted, and the end identifier must be written at the beginning of a new line. Also, the naming of identifiers must follow the naming rules of other tags in PHP: they can only contain letters, numbers, and underscores, and they must start with an underscore or non-numeric character.

It should be noted that although variables can be parsed in strings defined using delimiters, operations cannot be performed.

Example:


<?php
header("Content-type:text/html;charset=utf-8");
$website = &#39;php中文网&#39;;
$url = &#39;//m.sbmmt.com/&#39;;
$title = &#39;PHP 教程&#39;;
$str = <<<str
        <!DOCTYPE html>
        <html>
        <head>
            <title> $title </title>
        </head>
        <body>
            您正在访问的是:<strong style="color:red">$website</strong><br>
            网址:<a href="$url" target="_blank">$url</a>
        </body>
        </html>
str;
echo $str;
?>

There are several ways to declare string variables in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of There are several ways to declare string variables in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn