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 ?

.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.

okay… It means that htaccess does not play any role to optimize the code …so how can we optimize php code?

What do YOU mean by optimize?

i meant that how to rearrange the code .so the code should be less

That’s too vague a request to be able to give a reasonable answer. Here’s one way - use the Ternary operator.

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

// Ternary equivalent
echo ($a==$b ? 'Equal' : 'Not Equal');

[fphp]unset[/fphp] unused variables

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.

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.

  • 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.

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:

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 :slight_smile:

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).

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 … ?

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…