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.

4
  • A million requests per second? Commented Dec 15, 2017 at 4:23
  • Yes, just assume Commented Dec 15, 2017 at 4:24
  • Both ways are horrific, do you want to see a 3rd option? Commented Dec 15, 2017 at 4:27
  • Sure if it helps Commented Dec 15, 2017 at 4:29

2 Answers 2

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]);
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
Most frameworks use the same kind of view buffering. laraval, CI, Fat-Free, Cake, generally you only echo in views.
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.