Archive for the ‘PHP’ Category

Calculate Average in PHP Example

To calculate the average of numbers in PHP, we first need to add up all the item, then divide by the total number of items. In our example, we will add up the eight numbers 2, 4, 5, 3, 2, 9, 11, and 9.

First, the code example:

<?php
  $totalRatings = 2+4+5+3+2+9+11+9;
  $totalVotes = 8;
  $average = $totalRatings / $totalVotes;
  echo "Average: $average";
?>

Next, the output of the above PHP example will print the following to a web browser.

Average: 5.625

Since PHP is not strongly typed, the line of PHP code that performs the dividing of $totalRatings and $totalVotes (both integers) will correctly result in a double value.

Enable HTTP Compression in PHP

From my past tasks in improving a website’s load time, I’ve read in various forums that allowing the server to perform HTTP compression can significantly improve load time. The website I wanted to optimize was powered by PHP and runs of an Apache server hosted by LunarPages.

It all began when I ran the PHP site through useful Microsoft Tool (http://www.microsoft.com/search/Tools/default.aspx). I found that HTTP Compression was Not Enabled.

What I need to do is add the below script before the header loads in all PHP pages:

<?php
  if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
  {
    ob_start("ob_gzhandler");
  }
  else
  {
    ob_start();
  }
?>

After all this, I did notice some odd behavior in PHP and consequently removed the script. Though others who have used the script managed without problems, I still believe this is a great resource.

How to Create PHP Static Method

About a couple of weeks ago, I covered the basics on how to Access PHP Class Variables and Methods. This article is different because it deals with only the creation of a static method. Before we continue, we will insure we know the difference between a non-static class method and a static method.

Non-Static Method Static Method
1 Requires creating a class object Can be called without creating a class object
2 Can call on other static methods and variables Can only call on static methods and function

The real world example of the differences between a non static method and static method can be an old full sized computer tower. In the static method point of view, assume you only want to use the computer tower as a foot rest when you sit at your desk. For this, you do not need to turn on the computer (create a class object). This does not require you to load you operating system (no instantiating of class variables required). But, you can still kick up your feet. However, because you did not turn on your computer, you will be unable to access non-static methods (can only call on static method methods).

In the non-static method point of view, if you want the operation system, you would need to turn on the computer (create class object). What this does is load your operating system (instantiate class variables) so you can finally login to your computer (method of the class). Additionally, with the computer on, we can still kick our feet on the tower (can call both static and non static function).

Now how do we create a function with static methods in PHP? When we write the static method, we must use the keyword static. This method we create will always return true and is created in the common class.

class common {
   public static function myStaticFunction()
   {
      return true;
   }
}

Now that we have created the static function, we can call on the static method with the following snippet of code. The first term “common” is the name of the class. This is followed by two colons (::) followed by the method name.

$value = common::myStaticFunction();

And there we have it! We have successfully created and used our static method. To see the official PHP article on static, go to http://www.php.net/manual/en/language.oop5.static.php.