In a PHP file i am assigning a XML file to an input variable
Example:
$xmlfile = '<?xml version="1.0" encoding="UTF-8"?>
<creators>
<creator>
<creatorName>Ganesh</creatorName>
</creator>
</creators>
<identifier identifierType="id"&开发者_运维技巧gt;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.
How can i declare a variable ($input) in ''(single quotes) because declaring disturbs the structure.
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.
You have to use double quotes
or choose HEREDOC string.
$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
.
$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:
1) no need to escape all double quotes (not applicable to HEREDOC, ofcourse);
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.
3) there is less chance to make typo in variable name.
精彩评论