简体   繁体   中英

How can I make the website I'm working on load faster?

I am working on a website that I "inherited" in my job and it currently uses PHP/HTML/CSS/JS and it takes AGES to load, but the thing is without the PHP it loads really fast. I, however, am not very experienced with PHP so I don't know exactly where should I tweak it for it to work faster. First at the very top of the page I have this code to preload everything:

<?

?>

Then further down a table gets made using this code (which I know I can get better just don't know how exactly since tables on the db seem to be unrelated and they specifically asked me not to touch the db.

<?
        $count = 0;
        foreach ($domains as $row){
            $count++;
    ?>

a little of html goes here in between (mostly rows of a table) and then:

<? 
     } ?>

So after about 2 days messing with the site I know my html/css/js is optimized but the php keeps making me wait around 5 - 10 seconds for everything to load (sometimes even longer) which of course is unacceptable, could anybody please help me out here?

EDIT

I tried taking some of the connections outside from the foreach loop but I get an error if I do so =/

Use a profiler like xhprof or xdebug +kcachegrind to find the places of your code that take a long time (or are executed too often) and optimize them.

First you should start by stopping writing queries in the foreach loop.

Get rid of queries which use LIKE statements.

Then you should recheck the indexes for the given tables. Rule of thumb would be that all rows which appear in WHERE and GROUP BY clauses should be indexed ( well.. there are exceptions, but you should be able to figure on case-to-case base ).

And you could get rid of the switch statement and replace it with array lookup.

.. well.. my 2 cents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM