McLosys Creative
McLosys Creative

Reputation: 759

HTML select options creation : HTML vs PHP performance

I would like to create a select box in HTML which lists 1 to 15 options. Using HTML i can do it as follows :-

<select>      
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option>
    <option>14</option>
    <option>15</option>
</select>

I can simplify this long HTML using PHP as follows:-

<select>
<?php
    for($i=1;$i<=15;$i++){
        echo "<option>".$i."</option>";
    }
?>
</select>

Which is better in the view of performance ?

Upvotes: 1

Views: 408

Answers (5)

ern
ern

Reputation: 71

First one is better in performance..The reason is,html will not run in the server..User will make request and server will respond and return text file..

In the second one,php needs to run in server..User will make request php will run then server return the text file..

Note that:your current approach does'nt worth optimization..This answer is in theory..

enter image description here

Upvotes: 1

Phiter
Phiter

Reputation: 14992

PHP simply echoes HTML and throws it in the client's screen. So you're using server processing power to do this. If you simply send the plain HTML, your server won't need to make those calculations.

You can also do this in javascript using jQuery (you can do with pure javascript, but jQuery is simpler)

Which is a client-side calculation and will depend only on the client's processing power.

Example:

for (var i=1; i<=15; i++)
  {
    
    $("#select").append("<option value='" + i + "' > " + i + " </option>");
  
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="select">


</select>

This is more flexible than simply creating 15 selects by hand in the HTML file.

The only problem is if the client has Javascript disabled, but that's something else.

Upvotes: 0

Gouda Elalfy
Gouda Elalfy

Reputation: 7023

it is better in performance, because it is not translated by php:

<select>      
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option>
    <option>14</option>
    <option>15</option>
</select>

but you must assign the values by :

<option value='1'>1</option>
<option value='2'>2</option>
.......... and so on

Upvotes: 0

Aniruddha Chakraborty
Aniruddha Chakraborty

Reputation: 1867

PHP and HTML is a different thing.

If you want the select box to be dynamic then 2nd option is better.

But If you're sure that you'll never ever touch the select box again for modify then yes 1st one will be a good choice.

But 2nd one is the best because trust me it'll help you in many ways

Upvotes: 1

Sven van de Scheur
Sven van de Scheur

Reputation: 1903

Php will simply generate the same html and then transfer the output to the client so php will cause some overhead.

In terms of performance pure html is always faster, but with php it might be more maintainable (in some cases)

Upvotes: 1

Related Questions