Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep php output lines in one line

Tags:

html

css

php

I just made a phone number to time zone converter and it displays result as:

Requested Phone No.: +1 732 78782722

Country: United States

Expected Region: Newark, New Brunswick

Timezone: America/New_York

Date: 2015-08-05

Time: 01:51:03 am

What I want to do is place all these outputs in a single line. Here's my output code

      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>');
        }

Thanks for the help.

like image 890
Rahul Sharma Avatar asked Aug 05 '15 06:08

Rahul Sharma


2 Answers

Try this: If you want to pass that in a variable

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;

Or this if you want to pass in your object:

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;
like image 124
aldrin27 Avatar answered Nov 14 '22 22:11

aldrin27


You need to create a string variable and concatenate all your output to that string like below:-

$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
like image 28
Anant Kumar Singh Avatar answered Nov 14 '22 22:11

Anant Kumar Singh