SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    May 2014
    Location
    earth
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how can i optimize my php code ?

    Hello everyone,

    I know there is ample ways to optimize php code .. but i m in doubt .can we optimize our code through htaccess or can we optimize through code directly.If we optimize code through htaccess then how we can do ?

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    .htaccess has NOTHING to do with the php code.
    PHP is server side and has already been processed before it is served to the browser where .htaccess lives.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Member
    Join Date
    May 2014
    Location
    earth
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay... It means that htaccess does not play any role to optimize the code ..so how can we optimize php code?

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What do YOU mean by optimize?
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Member
    Join Date
    May 2014
    Location
    earth
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i meant that how to rearrange the code .so the code should be less

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's too vague a request to be able to give a reasonable answer. Here's one way - use the Ternary operator.

    PHP Code:
    // Typical If statement
    if ($a==$b) {
      echo 
    'Equal';
    } else {
      echo 
    'Not Equal';
    }

    // Ternary equivalent
    echo ($a==$b 'Equal' 'Not Equal'); 
    Ian Anderson
    www.siteguru.co.uk

  7. #7

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,821
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by macke View Post
    i meant that how to rearrange the code .so the code should be less
    Have you a code sample that you would like optimising?

    I think that code becomes optimised, streamlined and efficient the more you become familiar with the language.

    http://en.wikipedia.org/wiki/Program_optimization

  9. #9
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    There's no simple answer here, macke.

    The simplest way is to install an accelerator, which caches the byte code generated by a PHP file (PHP files are recompiled each time they are run, or for each browser request), among other things.

    Aside from this, the best thing you can do to "optimize" your code, is to actually debug it. Follow a tutorial on getting xdebug working, and have a look at the stack traces that are being created. What functions / operations are taking the longest? Usually you'll find things that you are doing repetitively / wrong that can be cleaned up.

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,263
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    http://java.dzone.com/articles/php-p...e-crash-course

    * Update PHP!
    * Use an opcode cache
    * Use autoloading
    * Optimize your sessions
    * Use a distributed data cache
    * Do blocking work in the background
    * Leverage HTTP caching
    * Optimize your favorite framework
    * Learn to how to profile code for PHP performance
    * Spend time improving the client side! In modern web applications most of the end-user experience time is spent waiting on the client.
    "First make it work. Then make it better."

  11. #11
    SitePoint Member
    Join Date
    May 2014
    Location
    earth
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay...Actually now days, i m working on my new website .<snip/> On that site i wanted to optimize my code. so anyone can review my site and then i could optimize my code .According to your feedback.
    Last edited by Mittineague; Jun 5, 2014 at 09:52. Reason: removing unnecessary link

  12. #12
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,821
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by macke View Post
    okay...Actually now days, i m working on my new website .<snip/>. On that site i wanted to optimize my code. so anyone can review my site and then i could optimize my code .According to your feedback.
    Please explain in detail what you mean by "On that site i wanted to optimize my code. so anyone can review my site and then i could optimize my code .According to your feedback"

    Your original question was about PHP code optimisation which is SERVER side and the HTML output is sent to the CLIENT's browser.

    As far as the HTML is concerned you may wish to remove some of the validation errors and warnngs:

    http://validator.w3.org/check?uri=ht...00=1&verbose=1
    Last edited by Mittineague; Jun 5, 2014 at 09:54. Reason: fixing quote

  13. #13
    SitePoint Member
    Join Date
    May 2014
    Location
    earth
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I meant that i m working on this site which I mentioned above .yes, I'm talking about php code optimization.My site has been developed in php so i want to know how to optimize my code .As SiteGuru already mentioned earlier through Example how to optimize php code.On that way, I'm talking about on this .

  14. #14
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,821
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by macke View Post
    I meant that i m working on this site which I mentioned above .yes, I'm talking about php code optimization.My site has been developed in php so i want to know how to optimize my code .As SiteGuru already mentioned earlier through Example how to optimize php code.On that way, I'm talking about on this .
    OK, if you supply a sample of the code that you would like optimising no doubt there will be numerous PHP gurus who could suggest improvements.

    Please don't forget to wrap the code in PHP or CODE tags

  15. #15
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But to confirm the answer to your question ... NOBODY can look at your website and comment on how to optimize the PHP code. That is because NOBODY CAN SEE your PHP code by looking at your website. Like John said above, PHP code is executed on the SERVER and the result is sent to the CLIENT (browser).
    Ian Anderson
    www.siteguru.co.uk

  16. #16
    SitePoint Member
    Join Date
    May 2014
    Location
    earth
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes..you are right siteguru.I made simple insertion code though that code i m inserting values on database but i heard we can also insert values through array ... is it possible to insert values through array ... ?

  17. #17
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Optimizing codes is a HUGE topic...

    Here are the techniques you should learn/understand first to fully optimize your codes..
    1. S.O.L.I.D Class
    2. Design Pattern
    3. Code Factoring
    4. YAGNI
    5. KISS
    6. DRY
    7. Code Smell
    8. Understanding Cache
    etc... etc... etc...

    .
    Quality codes are optimized and tested...
    Click here for inspiration..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •