a之间有什么区别。 a和一个{在变量的php回声中? [重复]
This question already has an answer here:
In PHP how do you know if you should use each form of echo, and what is the proper uses for each:
with the period:
echo"<div>
<img src='".$row['image']."'>
".$row['text']."
</div>
with the comma:
echo"<div>
<img src='",$row['image'],"'>
",$row['text'],"
</div>
with the {}:
echo"<div>
<img src='{$row['image']}'>
{$row['text']}
</div>
preset:
$image=$row['image'];
$text=$row['text'];
echo"<div>
<img src='$image'>
$text
</div>
More specifically I'm looking for the difference in how PHP will add the text to the HTML dump on the front end.
</div>
with period:
echo"<div>
<img src='".$row['image']."'>
".$row['text']."
</div>";
src='".$row['image']."'
denotes the concatenation of $row['image']
with comma:
echo"<div>
<img src='",$row['image'],"'>
",$row['text'],"
</div>";
^The comma ,
defines the string separation in the above example.
preset:
$image=$row['image'];
$text=$row['text'];
echo"<div>
<img src='$image'>
$text
</div>
^While this would still work with single '$image'
A better practice would be '".$row['image']."'
wrapped around quotes, i.e. below:
echo "<div>
<img src='".$image."'>
".$text."
</div>";
Note: Keeping it simple,
.
for concatenation and,
for separation.
^An Example:
<?php
$str = "Hawas" . "Ka" . "Pujaari";
echo $str;
$str2 = "Hawas, " . "Ka, " . "Pujaari";
echo $str2;
?>
.
concatenates strings (+
in most languages, but not PHP). PHP manual operators
,
separates arguments (echo can take more than 1 argument). PHP manual echo
In your first example
echo" ".$row['text']."
must be changed to
echo " ".$row['text']."
which is write syntax to concatenate two string.
For second example
echo" ",$row['text'],"
must be changed to echo " ",$row['text'],"
I don think this is valid syntax in php .
$image=$row['image']; $text=$row['text']; echo" $text
must be like
$image=$row['image']; $text=$row['text']; echo $text;