P. Lau
P. Lau

Reputation: 165

PHP Performance Between Call a Variable VS Direct Value

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.

Upvotes: 0

Views: 89

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46610

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]);

Upvotes: 1

JJJJ
JJJJ

Reputation: 1358

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?

Upvotes: 0

Related Questions