Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This question is really vague. When you want to optimize your script, you first check your database and try to optimize your algorithms. There aren't many pure PHP performance tips that are going to matter. Let's see :</p> <ul> <li><p>Concatening variables is faster than just putting them in a double-quotation mark string.</p> <pre><code>$var = 'Hello ' . $world; // is faster than $var = "Hello $world"; // or $var = "Hello {$world}"; </code></pre></li> </ul> <p>Yes, it's faster, but the second and third form are even more readable and the loss of speed is so low it doesn't even matter.</p> <ul> <li><p>When using a loop, if your condition uses a constant, put it before the loop. For instance :</p> <pre><code>for ($i = 0; $i &lt; count($my_array); $i++) </code></pre></li> </ul> <p>This will evaluate count($my_array) every time. Just make an extra variable before the loop, or even inside :</p> <pre><code>for ($i = 0, $count = count($my_array); $i &lt; $count; $i++) </code></pre> <ul> <li><p>The worst thing is definitely queries inside loops. Either because of lack of knowledge (trying to simulate a JOIN in PHP) or just because you don't think about it (many insert into in a loop for instance).</p> <pre><code>$query = mysql_query("SELECT id FROM your_table"); while ($row = mysql_fetch_assoc($query)) { $query2 = mysql_query("SELECT * FROM your_other_table WHERE id = {$row['id']}"); // etc } </code></pre></li> </ul> <p><strong>Never</strong> do this. That's a simple INNER JOIN.</p> <p>There are probably more, but really, it's not worth writing all of them down. Write your code, optimize later.</p> <p>P.S. I started writing this answer when there was none, there may be some things already said in links.</p> <p>Edit: for some reason, I can't format the code correctly. I really don't understand why.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload