0

I have a php script below: I can write it this 1st way:

echo"
<table class='fom'><tr class='xbo'><td class='ftd'>Name</td><td><input size='30' maxlength='30'></td></tr></table>" 
<table class='fom'><tr class='xbo'><td class='ftd'>Company</td><td><input size='30' maxlength='30'></td></tr></table>"  
<table class='fom'><tr class='xbo'><td class='ftd'>Contact</td><td><input size='30' maxlength='12'></td></tr></table>";

I also can write it this 2nd way:

$tb = "<table class='fom'><tr class='xbo'><td class='ftd'>";
$tZ = "</td></tr></table>";
echo"
 $tb."Name</td><td><input size='30' maxlength='30'>".$tz."" 
.$tb."Company</td><td><input size='30' maxlength='30'>".$tz.""  
.$tb."Contact</td><td><input size='30' maxlength='12'>".$tz."";

I prefer 2nd method cause it looks more tidy and wont squeeze my screen. The question is:

1) Will 2nd method slow down the PHP parser, I mean if the request towards this sript is very high...say milions/s.

2) Is there any way I can check the speed performance of this script parsing on my MAMP?

Need some expert opinions.

P. Lau
  • 165
  • 1
  • 11

2 Answers2

1

Option 3 - Don't use single quotes in HTML and simply break out of PHP to do your HTML. If you need to insert variables then do so. You should also NOT be using <tables> for page layout instead use <divs> with CSS.

It will make it much easier to read then concatenating a bunch of HTML with your PHP variables as strings, you also maintain HTML formatting, and it does not affect performance enough that you could possibly notice it.

?>
<div class="fom">
    <div class="xbo">
        <label class="ftd">Name</label>
        <input size="30" maxlength="30" name="name" value="<?= (!empty($_POST['name']) ? htmlentities($_POST['name']) : '') ?>">
    </div>
</div>
<div class="fom">
    <div class="xbo">
        <label class="ftd">Company</label>
        <input size="30" maxlength="30" name="company" value="<?= (!empty($_POST['company']) ? htmlentities($_POST['company']) : '') ?>">
    </div>
</div> 
<div class="fom">
    <div class="xbo">
        <label class="ftd">Contact</label>
        <input size="30" maxlength="12" name="contact" value="<?= (!empty($_POST['contact']) ? htmlentities($_POST['contact']) : '') ?>">
    </div>
</div>
<?php

You can even abstract further out and separate your HTML from your PHP logic by using views.

You would have a simple function which loads your HTML which you pass your PHP variables to, it then returns the rendered version for storing into a variable (partial) or echoing out.

function view($view = '', $data = array()) {

    if (file_exists($view) === false) {
        return 'Partial view not Found';
    }

    if (!empty($data)) {
        extract($data);
    }

    ob_start();
    require($view);
    return ob_get_clean();
}

You can then use it like:

echo view('./views/homepage.php', ['result' => $result]);
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • If break out of PHP, does it means the PHP GENERATED HTML or HTML before it will be send to the browser first? Eventhough there are still other script not interpreted? – P. Lau Dec 15 '17 at 05:24
  • Breaking out of php is like echoing without the echo and wrapping into quotes, yeah its starts the output like an echo.. if you have echoed before that it wont effect the order etc. Using a view like the example does not as the ob_* functions catch it and return it as a string. – Lawrence Cherone Dec 15 '17 at 05:28
  • Most frameworks use the same kind of view buffering. [laraval](https://github.com/laravel/framework/blob/bd352a0d2ca93775fce8ef02365b03fc4fb8cbb0/src/Illuminate/View/Concerns/ManagesComponents.php#L46), [CI](https://github.com/bcit-ci/CodeIgniter/blob/432a9130059873551d1cff3e40d1d8432f552b96/system/core/Loader.php#L958), [Fat-Free](https://github.com/bcosca/fatfree/blob/e9aec879272c7474294c51c6c00802615afa32a6/lib/base.php#L1609), [Cake](https://github.com/cakephp/cakephp/blob/b27ebbd6a4e9b6062bd45e13a439b7ab7f29d0af/src/View/ViewBlock.php#L93), generally you only echo in views. – Lawrence Cherone Dec 15 '17 at 05:40
0

You can format in this way.

$td_class = 'class="ftd"';
echo '<table class="fom"><tr class="xbo"><td class="ftd">';

echo '<tr>
        <td ' . $td_class .'>Name</td>
        <td><input size="30" maxlength="30"></td>
      </tr>';
echo '<tr>
        <td ' . $td_class . '>Company</td>
        <td><input size="30" maxlength="30"></td>
      </tr>';
echo '<tr>
        <td ' . $td_class . '>Contact</td>
        <td><input size="1" maxlength="12"></td>
      </tr>';

echo '</table>';

Instead of using double quote" use single quote ' What is the difference between single-quoted and double-quoted strings in PHP? . And you can also read Speed difference in using inline strings vs concatenation in php5?

Bluetree
  • 1,324
  • 2
  • 8
  • 25