Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,754 members, 7,817,085 topics. Date: Saturday, 04 May 2024 at 04:07 AM

Some optimization tips for php - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Some optimization tips for php (860 Views)

Bincom Academy 95% Scholarship For PHP/MYSQL Training January Session / Help On Login Code For Php N How To Store On Data Base. / [Code Request] Code for PHP Login And Signup (2) (3) (4)

(1) (Reply) (Go Down)

Some optimization tips for php by grandtheft: 6:33am On Sep 20, 2013
1) ++$i is faster than $i++. You should use this is scenarios where it doesn't matter if you do a post or pre increment, because both of they are not the same. Same applies to pre decrement and post decrement.
2)Single quotes is faster than double quote because php searches for variables in a double quote. If you don't have variables in the double quotes consider changing it to single quotes.
3) for($i = 0; $i < count($arr); $i++) { is very bad because the count function gets called on each iteration. Rather declare a variable outside the loop like this: $count = count($arr); for($i = 0; $i < $count; $i++) {
Re: Some optimization tips for php by grandtheft: 7:02am On Sep 20, 2013
4)Rather than search an array for a value. Why not make the value the key then use the isset method ?. Here is what am saying:
<?php
$arr = array('apple' => 1, 'pineapple' => 1, 'orange' => 1, 'mango' => 1, 'pawpaw' => 1);
echo (isset($arr[$_GET['fruitname']]) ? 'Fruit Found' : 'Fruit Not Found');
?>
that is better, we all know isset is very fast. That was the same technique i used to build the unofficial .

5)copying variables just because you want a shorter name is totally wrong. Except you have a reason, because it consumes more space for nothing.
Re: Some optimization tips for php by spikesC(m): 11:06am On Sep 20, 2013
Do not use classes everywhere, even though you don't need them, just cos u wanna go OOP.

Including files and using classes are very resource intensive operations.

1 Like

Re: Some optimization tips for php by Nobody: 2:25pm On Sep 20, 2013
spikes C: Do not use classes everywhere, even though you don't need them, just cos u wanna go OOP.

Including files and using classes are very resource intensive operations.

you can use the SPL Autoloader and Namespaces, they only get triggered when a class is instantiated
Re: Some optimization tips for php by CHARLYX9: 3:25pm On Sep 20, 2013
pc guru:

you can use the SPL Autoloader and Namespaces, they only get triggered when a class is instantiated
Recommend 6 good beginners php book for me with 840 pages abeg.
Re: Some optimization tips for php by Nobody: 3:34pm On Sep 20, 2013
CHARLYX9:
Recommend 6 good beginners php book for me with 840 pages abeg.
Why do you need six and why the emphasis on pages, i used only one book Php 5 power programming it takes on advanced php and oop

1 Like

Re: Some optimization tips for php by Ajibel(m): 4:41pm On Sep 20, 2013
CHARLYX9:
Recommend 6 good beginners php book for me with 840 pages abeg.


shocked shocked cheesy cheesy
Re: Some optimization tips for php by spikesC(m): 2:57am On Sep 21, 2013
pc guru:

you can use the SPL Autoloader and Namespaces, they only get triggered when a class is instantiated

The advice is not to use classes at all when it's not necessary.

BTW, the SPL Autoloader is even worst, talking about resource usage.
Re: Some optimization tips for php by Nobody: 5:20am On Sep 21, 2013
spikes C:

The advice is not to use classes at all when it's not necessary.

BTW, the SPL Autoloader is even worst, talking about resource usage.
so if am to understand you, in order to gain performance i should write all my codes in one file and not use classes, no thanks __autoload and SPLAutoloaders only activate when a class is called, i haven't encountered any performance issue on my part here besides its PSR-0 its the standard all my projects at work are OOP anyway so i don't know much about procedural projects.
Re: Some optimization tips for php by grandtheft: 6:47am On Sep 21, 2013
pc guru:
so if am to understand you, in order to gain performance i should write all my codes in one file and not use classes, no thanks __autoload and SPLAutoloaders only activate when a class is called, i haven't encountered any performance issue on my part here besides its PSR-0 its the standard all my projects at work are OOP anyway so i don't know much about procedural projects.
he didn't say All you code.
Re: Some optimization tips for php by Nobody: 6:51am On Sep 21, 2013
grandtheft: he didn't say All you code.

Ok sorry i misread that ,sure OOP is not neccessary , also another tip using Output Buffering for caching puproses like Gzip and HTTP Compression, Database result cache and some others. Install APC too works also.
Re: Some optimization tips for php by grandtheft: 8:23am On Sep 21, 2013
Also do not do mysql queries in a loop, you can store all the queries in a variable in the loop then do the query outside the loop
Re: Some optimization tips for php by grandtheft: 8:51am On Sep 21, 2013
Use str function over most regex functions. Except you really require a regex.
Re: Some optimization tips for php by Elvisten10(m): 5:44pm On Sep 21, 2013
pc guru: elvisten built the api, unless this is elvisten cheesy seun don strike lol.
lmao

(1) (Reply)

Android Development / We Pride Ourselves In Being A Company That Develop Premium Web And Mobile / Modern Web Technology

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.