Home >Backend Development >PHP Problem >There are several ways to declare string variables in php
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 =
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php declaration string Variable
##1. Use double quotes
$变量名="字符串内容";Example:
<?php $str="hello"; var_dump($str); ?>
2. Use single quotes
$变量名='字符串内容';Example:
<?php header("Content-type:text/html;charset=utf-8"); $str='hi'; var_dump($str); ?>Single quotes are the most efficient, but single quotes do not parse the contained variables.
$a='aaa'; $b='$abbb'; echo $b;
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.
<?php header("Content-type:text/html;charset=utf-8"); $website = 'php中文网'; $url = '//m.sbmmt.com/'; $title = 'PHP 教程'; $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; ?>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!