【问题标题】:PHP echo to remain on the same line after HTMLPHP 回显在 HTML 之后保持在同一行
【发布时间】:2016-08-15 19:38:13
【问题描述】:
(<?php _e( 'Up to', 'test' ); ?> &pound;
<?php $current_price = get_field( 'price_details_price_b', 'option' );
$exchange_rate = get_field( 'price_details_exchange_rate', 'option' );
$uk_price = ($current_price * $exchange_rate);
echo $uk_price;
?>)

此代码在新行上输出回显。我不希望发生这种情况,也不希望 PHP 在井号符号之后启动,那么解决方案是什么?

我知道我可以通过在磅符号之后和 PHP 标记之前添加 HTML 注释 &lt;!-- --&gt; 来做到这一点,但想知道是否有更好的解决方案?

【问题讨论】:

  • 比较常见的是先获取自己需要的数据,然后再显示。你应该省略英镑,直到你计算出价格,然后echo '&amp;pound;'.$uk_price;
  • 如果您的 PHP 代码之外有换行符,它们当然会作为新行输出。 PHP 对此无能为力。

标签: php html


【解决方案1】:

也许先做你的抓取和计算:

<?php
  $current_price = get_field( 'price_details_price_b', 'option' );
  $exchange_rate = get_field( 'price_details_exchange_rate', 'option' );
  $uk_price = ($current_price * $exchange_rate);
?>

然后在适当的地方合并输出:

(<?php
   _e( 'Up to', 'test' );
   echo '&pound;' . $uk_price;
?>)

【讨论】:

    【解决方案2】:

    你为什么不能这样做

       (<?php _e( 'Up to', 'test' ); ?> &pound <?php
       $current_price = get_field( 'price_details_price_b', 'option' );
    

    ?还是一开始就不退出 PHP 模式?

       (<?php _e( 'Up to', 'test' ); echo '&pound;';
       $current_price = get_field( 'price_details_price_b', 'option' );
    

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 2019-01-03
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      相关资源
      最近更新 更多