Improve Performance By Avoiding Day-To-Day Mistakes In Coding PHP

      Save time and speed scripts by working with object properties directly, rather than writing naive setters and getters.

class test {
         public $name = '';
   
         public function setName($name) {
            $this->name = $name;
         }
 
         public function getName() {
            return $this->name;
         }
      }

   //Notice that setName() and getName() do nothing more than store and return the name property, respectively. (SLOW)

      $rover = new dog();

      $rover->setName(‘rover’);

      echo $rover->getName();

      //Setting and calling the name property directly can run up to 100% faster, as well as cutting down on development time.(FAST)

      $rover = new dog();

      $rover->name = ‘rover’;

      echo $rover->name;

2. Don’t copy variables for no reason:

      An attempt to make code cleaner by copying predefined variables to variables with shorter names before working with them, actually results in,is doubled memory consumption (when the variable is altered), and therefore, slow scripts.

      //if inserted 512KB worth of characters, result in is nearly 1MB of memory being used.(SLOW)

      $description = strip_tags($_POST[‘description’]);

      echo $description;

      //Simply do this operation inline and avoid the extra memory consumption(FAST)

      echo strip_tags($_POST[‘description’]);

3. Avoid doing SQL queries within a loop:

      //multiple round trips to the database which significantly slow the scripts.(SLOW)

      foreach ($userList as $user) {

         $query = ‘INSERT INTO users (first_name,last_name) VALUES(“‘ . $user[‘first_name’] . ‘”, “‘ . $user[‘last_name’] . ‘”)’;

         mysql_query($query);

      }

      Produces: INSERT INTO users (first_name,last_name) VALUES(“John”, “Doe”)

      //Combine the data into a single database query.(FAST)

      $userData = array();

      foreach ($userList as $user) {

         $userData[] = ‘(“‘ . $user[‘first_name’] . ‘”, “‘ . $user[‘last_name’] . ‘”)’;

      }

      $query = ‘INSERT INTO users (first_name,last_name) VALUES’ . implode(‘,’, $userData);

      mysql_query($query);

      Produces:

      INSERT INTO users (first_name,last_name) VALUES(“John”, “Doe”),(“Jane”, “Doe”)…

4. When using a loop, if condition uses a constant, put it before the loop. For instance:

      //This will evaluate count($my_array) every time. (SLOW)

      for ($i = 0; $i < count($my_array); $i++) {}

      //Just make an extra variable before the loop, or even inside :(FAST)

      $count = count($my_array);

      for ($i = 0, $count; $i < $count; $i++) {}

      OR

      for ($i = 0, $count = count($my_array); $i < $count; $i++) {}

5. Single quotes (’) with concatenation is faster than putting your variables inside a double quote (”) string:

      //In double quotes, php constantly looks through the string for variable names to check, parse and call.(SLOW)

      $var = “Hello $world”;

      $var = “Hello {$world}”;

      //In single quotes, php does not process anything within it.(FAST)

      $var = ‘Hello ‘ . $world;

6. echo is faster than print:

      print is a function which returns 1 if statement successfully executed else returns 0 and as it is a function it needs time to execute. (SLOW)

      print is good when you need to use it as a function.

      echo execute very fast.(FAST)

7. Use absolute paths in includes and requires:

      //More time is spent on resolving the OS paths.(SLOW)

      include(‘test.php’);

      //Less time is spent on resolving the OS paths.(FAST)

      include(‘/var/www/html/your_app/test.php’);

150 150 Burnignorance | Where Minds Meet And Sparks Fly!