Php回声,印刷方式的改变


Php echo , change of manner of printing

我有一个简单的问题:

我有这段代码,一切都可以,但我需要的是用不同的方式打印它,我已经尝试了一些东西,但问题是,我没有收到任何错误,但结果没有走正确的路,

我尝试打印的代码是这样的:

<ul class='star-rating' id="star-rating-<?php echo $star['id'];?>">
<?php /* getRating($id) is to generate current rating */?>
  <li class="current-rating" id="current-rating-<?php echo $star['id'];?>" style="width:<?php echo getRating($star['id'])?>%"><!-- will show current rating --></li>
  <?php 
  /* we need to generate 'id' for star rating.. this 'id' will identify which data to execute  */
  /* we will pass it in ajax later */
  ?>
  <span class="ratelinks" id="<?php echo $star['id'];?>">
  <li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li>
    <li><a href="javascript:void(0)" title="1 star and a half out of 5" class="one-star-half">1.5</a></li>
  <li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li>
    <li><a href="javascript:void(0)" title="2 star and a half out of 5" class="two-star-half">2.5</a></li>
  <li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li>
    <li><a href="javascript:void(0)" title="3 star and a half out of 5" class="three-star-half">3.5</a></li>
  <li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li>
    <li><a href="javascript:void(0)" title="4 star and a half out of 5" class="four-star-half">4.5</a></li>
  <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li>
  </span>
</ul>

当我尝试这个时:

echo"<ul class='star-rating' id='star-rating-'". $star['id'].">
 <li class='current-rating' id='current-rating-".  $star['id']." style='width:". getRating($star['id'])."'%></li>";
 echo'<span class="ratelinks" id="'. $star['id'].'">
  <li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li>
    <li><a href="javascript:void(0)" title="1 star and a half out of 5" class="one-star-half">1.5</a></li>
  <li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li>
    <li><a href="javascript:void(0)" title="2 star and a half out of 5" class="two-star-half">2.5</a></li>
  <li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li>
    <li><a href="javascript:void(0)" title="3 star and a half out of 5" class="three-star-half">3.5</a></li>
  <li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li>
    <li><a href="javascript:void(0)" title="4 star and a half out of 5" class="four-star-half">4.5</a></li>
  <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li>
  </span>
</ul>';

echo 和字符串之间缺少一个空格。字符串中也有一些错误。

echo "<ul class='star-rating' id='star-rating-". $star['id']."'>
 <li class='current-rating' id='current-rating-".  $star['id']."' style='width:". getRating($star['id'])."%'></li>";
echo '<span class="ratelinks" id="'. $star['id'].'">
  <li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li>
    <li><a href="javascript:void(0)" title="1 star and a half out of 5" class="one-star-half">1.5</a></li>
  <li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li>
    <li><a href="javascript:void(0)" title="2 star and a half out of 5" class="two-star-half">2.5</a></li>
  <li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li>
    <li><a href="javascript:void(0)" title="3 star and a half out of 5" class="three-star-half">3.5</a></li>
  <li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li>
    <li><a href="javascript:void(0)" title="4 star and a half out of 5" class="four-star-half">4.5</a></li>
  <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li>
  </span>
</ul>';

我必须说,我很想知道你为什么要做出这种改变的原因?我建议用单引号'括起来的字符串。这样,您仍然可以添加带有预期双引号"的 HTML 属性,而不必对其进行转义。

例:

echo '<ul class="star-rating" id="star-rating=' . $star['id'] . '">
  <li class="current-rating" id="current-rating-' . $star['id'] . '" style="width:' . getRating($star['id']) . '%"></li>';

在处理大段 HTML 时,我总是建议使用 heredoc 并使用 {} 来替换变量,即 {$star['id']} .

您需要在此文档之前计算您的星级。

echo <<<"EOD"
<ul class='star-rating' id="star-rating-{$star['id']}">
<?php /* getRating($id) is to generate current rating */?>
  <li class="current-rating" id="current-rating-{$star['id']}" style="width:{$star_rating['id']}%"><!-- will show current rating --></li>
  <span class="ratelinks" id="<?php echo $star['id'];?>">
  <li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li>
    <li><a href="javascript:void(0)" title="1 star and a half out of 5" class="one-star-half">1.5</a></li>
  <li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li>
    <li><a href="javascript:void(0)" title="2 star and a half out of 5" class="two-star-half">2.5</a></li>
  <li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li>
    <li><a href="javascript:void(0)" title="3 star and a half out of 5" class="three-star-half">3.5</a></li>
  <li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li>
    <li><a href="javascript:void(0)" title="4 star and a half out of 5" class="four-star-half">4.5</a></li>
  <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li>
  </span>
</ul>
EOD;