【问题标题】:how to keep php output lines in one line如何将php输出行保持在一行
【发布时间】:2015-08-05 06:09:19
【问题描述】:

我刚刚将电话号码转换为时区转换器,结果显示为:

Requested Phone No.: +1 732 78782722

Country: United States

预期地区:纽瓦克、新不伦瑞克

Timezone: America/New_York

Date: 2015-08-05

Time: 01:51:03 am

我想要做的是将所有这些输出放在一行中。 这是我的输出代码

      if(!empty($record['country_name'])) {
            $this->display('<strong>Country:</strong> ' . $record['country_name']);
        }

        if(!empty($record['city'])) {
            $this->display('<strong>Expected Region:</strong> ' . $record['city']);
        }

        //echo json_encode($date); 

        if(!empty($record['zone_name'])) {
            $this->display('<strong>Timezone:</strong> ' . $record['zone_name']);
            $this->display('<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>');
            $this->display('<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>');
        }

感谢您的帮助。

【问题讨论】:

  • 能不能把你正在使用的显示功能的代码也显示一下?
  • 我认为他正在使用 Smarty

标签: php html css


【解决方案1】:

试试这个:如果你想在变量中传递它

if(!((empty($record['country_name']) && empty($record['city']) && empty($record['zone_name'])) {
    $var = '<strong>Country:</strong> ' . $record['country_name'] . '<strong>Expected Region:</strong> ' . $record['city'] .  '<strong>Timezone:</strong> ' . $record['zone_name']. '<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>' . '<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>';
}

echo $var;

或者如果你想传入你的对象:

if(!((empty($record['country_name']) && empty($record['city']) && empty($record['zone_name'])) {
    $this->display('<strong>Country:</strong> ' . $record['country_name'] . '<strong>Expected Region:</strong> ' . $record['city'] .  '<strong>Timezone:</strong> ' . $record['zone_name']. '<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>' . '<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>');
}

return $this;

【讨论】:

    【解决方案2】:

    您需要创建一个字符串变量并将所有输出连接到该字符串,如下所示:-

    $result = ''; // create an empty string
    
    if(!empty($record['country_name']) && !empty($record['city']) && !empty($record['zone_name'])){
    
    $result = '<strong>Country:</strong> ' . $record['country_name'].' <strong>Timezone:</strong> '. $record['city'].' <strong>Timezone:</strong> '.$record['zone_name'].' <h2><strong>Date:</strong> '. date('Y-m-d') . '</h2>'.' <h2><strong>Time:</strong> '. date('H:i:s a') . '</h2>';
    
    }
    echo $result; // print output
    

    【讨论】:

    • 您的帖子确实有帮助,但问题是输出之间没有空格@AK-Sonu
    • 您可以为此目的使用&amp;nbsp;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2020-05-16
    • 2016-12-17
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多