简体   繁体   English

单引号和双发行

[英]Single quotation and double issue

In a PHP file i am assigning a XML file to an input variable 在PHP文件中,我正在将XML文件分配给输入变量

Example: 例:

$xmlfile = '<?xml version="1.0" encoding="UTF-8"?>
<creators>
    <creator>
        <creatorName>Ganesh</creatorName>
    </creator>
</creators> 
<identifier identifierType="id">Dynamic input($input)</identifier>'; 

In the part of Dynamic input i want to declare a variable like $input which would based on the previous value. 在动态输入部分,我想声明一个像$ input这样的变量,它将基于先前的值。

How can i declare a variable ($input) in ''(single quotes) because declaring disturbs the structure. 我如何在''(单引号)中声明变量($ input),因为声明会干扰结构。

You can concatenate it in: 您可以串联

$xmlfile = '<?xml version="1.0" encoding="UTF-8"?>
<creators>
    <creator>
        <creatorName>Toru, Nozawa</creatorName>
    </creator>
</creators> 
<identifier identifierType="id">Dynamic input(' . $input. ')</identifier>'; 

Fair warning, XML cannot hold all values, so you might need to escape the data depending on where it comes from -- for instance, you might not want any angle brackets, and you certainly need to cleanse any control characters. 公平警告,XML不能保存所有值,因此您可能需要根据数据的来源对数据进行转义-例如,您可能不需要任何尖括号,并且当然需要清除所有控制字符。

You have to use double quotes or choose HEREDOC string. 您必须使用double quotes或选择HEREDOC字符串。

$xmlfile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<creators>
    <creator>
        <creatorName>Toru, Nozawa</creatorName>
    </creator>
</creators> 
<identifier identifierType=\"id\">Dynamic input($input)</identifier>";  

HEREDOC string : ( <<<ABC is opening identifier and the closing identifier must begin in the first column of the line - ABC . HEREDOC字符串:( <<<ABC是开始标识符,结束标识符必须在行ABC的第一列中开始。

$xmlfile = <<<ABC
<?xml version="1.0" encoding="UTF-8"?>
<creators>
    <creator>
        <creatorName>Toru, Nozawa</creatorName>
    </creator>
</creators> 
<identifier identifierType="id">Dynamic input($input)</identifier>
ABC;

Try: 尝试:

$xmlfile = '<?xml version="1.0" encoding="UTF-8"?>
<creators>
    <creator>
        <creatorName>Toru, Nozawa</creatorName>
    </creator>
</creators> 
<identifier identifierType="id">Dynamic input('.$input.')</identifier>'; 

I prefer to use concationation instead of including variables in double quotes or HEREDOC (but HEREDOC goes next in my preferences list if text is long enough) because of three reasons: 我宁愿使用缩排词而不是在双引号或HEREDOC中包含变量(但如果文本足够长,则HEREDOC在我的首选项列表中排在第二位),原因有以下三个:
1) no need to escape all double quotes (not applicable to HEREDOC, ofcourse); 1)无需转义所有双引号(当然不适用于HEREDOC); 2) PHP interpreter doesn't need to parse long text over and over again within every request to find any variable inside and replace it with its value. 2)PHP解释器不需要在每个请求中一遍又一遍地解析长文本,以查找其中的任何变量并将其替换为其值。
3) there is less chance to make typo in variable name. 3)在变量名中打错字的机会更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM