Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP optimization - include() or not

Tags:

php

I have two optimization question about the function include.

  1. Is it better to use a single php file and include it or use several little files and include them? Which one will be faster?

  2. For example, I use a PHP file with mysql_connect and all the db connection stuff. Then I include it when I need it. But will it be faster to just write the code when I need it and not include anything?

Also if someone has the actual numbers, I will be a nice plus.

like image 922
user1648791 Avatar asked Dec 15 '22 19:12

user1648791


2 Answers

  1. The differences will be trivial.

  2. Don't repeat yourself. Do not put connection information in each file over and over again. Including sounds fine in your case.

  3. Stop making use of mysql_*(). Use PDO or MySQLi instead.

You're talking about micro-optimalisation, while it's probably better to start thinking about object oriented programming instead.

like image 63
Sherlock Avatar answered Dec 28 '22 04:12

Sherlock


Pick any decent-sized open source project. Like WordPress, Joomla, Drupal for instance. Now check if they have a single gigantic-everything-goes-in-there file or if they have split it into small, maintainable components.

Answer: favor maintainability first. When you hit a bottleneck, you'll be able to find it and address in a much easier way.

like image 20
Vincent Mimoun-Prat Avatar answered Dec 28 '22 05:12

Vincent Mimoun-Prat