Category: PHP

Find The Number Of Days For A Given Month With PHP

10 October, 2008 | PHP | No comments

There are two ways to find out the number of days for a given month. The first is to use the date() function in conjunction with the mktime() function to create a date and format this value as the number of days in a given month.

$monthDays = date("t",mktime(0,0,0,12,1,2008));

The second way is to use the function cal_days_in_month(). This function takes three parameters.

  • Canelday: There are a number of different forms of calendar to chose from. The one most English speaking people are interested in is the CAL_GREGORIAN calendar. Take a look at a full list of calendar constands.
  • Month: An integer representing the month (1 - 12).
  • Year: An integer representing the year.

So to find out the number of days in the month of December of 2008 you could use the following.

$monthDays = cal_days_in_month(CAL_GREGORIAN, 12, 2008);

PHP Function To Work Out Factorial Numbers

8 October, 2008 | PHP | No comments

Factorial of a number is defined as the product of the number and all of the numbers small than it. So if you take the number 4 the factorial of that number is 24 or 1 x 2 x 3 x 4.

Factorials are useful for a number of applications, for example when working out how many times a set of objects can be combined in different ways.

Use the following PHP function to work out the factorial of any given number. The first thing it does it make sure that the number is greater than 1 because the factorial of the number 1 is 1.

function factorial($number){
 $result = 1;
 if($number > 1){
  for($i = 2; $i >= $number; $i++){
   $result *= $i;
  }
 }
 return $result;
}

The function works by using the *= operator, which does a calculation between the left and right sides (in this case multiplication) and stores the result on the left hand side. This operator is much like the += operator, and works in the same way.

An alternative of this is the GMP function gmp_fact(). GMP is a set of maths functions that can be used to do some special things like add very large numbers together. It has been part of the PHP core since version 4.0.4.

Beware that factorial numbers can get very large, very quickly. For example, the factorial of the number 50 is 30,414,093,201,713,375,576,366,966,406,747,986,832,057,064,836,514,787,179,557,289,984.

Get Fibonacci Numbers Using PHP

7 October, 2008 | PHP | No comments

Fibonacci numbers not only have a few uses, but are also quite a nice little number sequence in themselves.

The sequence starts at 0, the next number is 1, and every number after that is the sum of the last two numbers. So the third number is 1, and the second number is 2.

To create this number sequence in PHP we need to create the first two items in the array. As we know that these are 0 and 1 we can create the array like this.

$fibarray = array(0,1);

To create the third number we just add the first two numbers together.

$fibarray[2] = $fibarray[0]+$fibarray[1];

This can be continued for as much as we want if we add it to a loop.

for($i=0;$i<=10;++$i){
 $fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];
}

This will create an array with the following values.


Array
(
 [0] => 0
 [1] => 1
 [2] => 1
 [3] => 2
 [4] => 3
 [5] => 5
 [6] => 8
 [7] => 13
 [8] => 21
 [9] => 34
 [10] => 55
)

Following on from this we can get a Fibonacci sequence to any position we want by using the following function.

function fibonacciSequence($pos){
 $fibarray = array(0,1);
 for($i=0;$i<=$pos;++$i){
  $fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];
 }
 return $fibarray;
}

We can also create a very similar function that will only return a number at a particular position.

function fibonacciSequence($pos){
 $fibarray = array(0,1);
 for($i=0;$i<=$pos;++$i){
  $fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];
 }
 return $fibarray[$pos];
}

For example, if we pass this function the number 56, the result would be 225,851,433,717.

PHP Script To Turn Image Into ASCII Text

6 October, 2008 | PHP | No comments

Use the following snippet to convert any jpeg image into the equivalent image in ASCII format. It works by loading an image using the PHP GD2 library function ImageCreateFromJpeg() and then figures out the height and width of it. It then uses these values to loop through every pixel in the image and figures out the colour of that pixel. It uses this value to create a <span> element that uses the text colour of a # to change the colour of the text.

An additional time (and space) saver for this function is that it detects any pixels that are just off white and simply displays a &nbsp; character instead.

$img = ImageCreateFromJpeg('logo.jpg');
 
// get height and with of the image.
$width = imagesx($img);
$height = imagesy($img);
 
echo '<pre style="font-size:1px;">';
for($h=0;$h<$height;$h++){
 for($w=0;$w<=$width;$w++){
  if($w == $width){
   echo '<br>';
   continue;
  }
  // get image at pixel location
  $rgb = ImageColorAt($img, $w, $h);
  // convert colour into usable format
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> 8 ) & 0xFF;
  $b = $rgb & 0xFF;
  // check for white/off-white colour
  if($r > 200 && $g > 200 && $b > 200){
   echo '&nbsp';
  }else{
   echo '<span  style="color:rgb('.$r.','.$g.','.$b.');">#</span>';
  }
 }
}
echo '</pre>';

As an example, take the following, quite recognisable, image.

This is transformed into the text on the following page.

The Google logo in ASCII format

Beware that although this works will for small image sizes if you try to convert a very large image you will find that the script takes a long time. This is because it looks at every pixel in turn and converts the colour into something usable, so if there are a lot of pixels it takes a long time to look at every one. Not only this, but you will also find that the size of the page generated will grow significantly due to all of the span elements being created.

Planet PHP

2 October, 2008 | PHP Websites | No comments

Planet PHP is a feed aggregator blog specifically for PHP related blogs. There are a good number of blogs that the site uses to update with so there is always plenty of stuff going up on the site. The site is not run by PHP, and is an independent project run by Christian Stocker and Tobias Schlitt.

Manage MySQL Databases With phpMyAdmin

The good thing about this site is that it saves having to go through all of the blogs in turn to see what is interesting in the world of PHP. If you run a PHP blog you can submit your site to the list and get your articles included in the updates.