Jan 062008
 

PHP is not a very strict language when it comes down to data types.
Strings:

$string = "This is a string";
 

Strings are values that contain characters. This can include basically everything you can type on your keyboard.

Integers:

$string = 1;
 

Integers are values that just contain numbers. Notice in the example above that there are no quotes?
Well, you can write an integer like that, or like this:

$string = "1";
 

PHP automatically converts numbers contained in a string with no other characters into a integer. The only time you need to be careful of this is when you are using operators (I will discuss that in another post).

Boolean:

$string = true;
$string2 = false;
 

Booleans are values that contain either true or false. They can be used in if statements like:

$string = true;
if ($string)//checks if $string is true
 {
 echo '$string is true';
 }
else
 {
 echo '$string is false';
 }
 

Hope that helps you understand data types in PHP better. Obviously I have just scratched the surface in my explanations.
Feel free to comment and ask any questions :D

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Jan 062008
 

Dynamic images (as I call them) are images that change depending on a condition.
A regular image URL is something like:
#1

<img src="http://www.papaface.com/blog/wp-content/themes/papaface/images/beer.png" />
 

However a Dynamic Image's URL could be something like:
#2

<img src="http://www.papaface.com/blog/somephpfile.php" />
 

Notice the difference?
Example 1's URL is actually an image file, however example 2's URL is actually a PHP file producing a dynamic image in this case.

These dynamic images are useful if you want to change the image produced at any point.

Lets say we are providing a link checking service and I want to offer the visitors the ability to put an image on their site once they've used the link checking service. The image will change to green when the link is valid, and red when it is not.

<img src="http://www.papaface.com/blog/resources/dynamicimage/example.php?id=22451" />
 

Now every time the image is loaded the script can actually look for link id "22451" in a database. It can then check the image for validity and produce a green or red image like so:

This is the code that would allow you to do this:

<?php  
       
        $linkid = $_GET['id']; //this is the link id that the script needs to look for in the database
       
        //you would open a mysql connection here
       
        $get = @mysql_query("SELECT `link` FROM `tbllinks` WHERE `linkid`='".mysql_real_escape_string($linkid)."'"); //@ suppresses the error message (this is usually a bad coding practice, but in this case we do not want anything other than an image displayed to the user.

        list($url) = mysql_fetch_array($get);
        /*The code below checks the link. If it is a valid and working link it will return a 200 code.*/
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch);
        $info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if      ($info == "200")
                $colour = "green";   //link is valid
        else
                $colour = "red"; //link is NOT valid
        }

    //code above decided if it should be green or red and produced $colour
   
        header("Content-type: image/jpg");
        $im     = imagecreatefromjpeg($colour.".jpg"); //tells the script what image to source locally (green.jpg/red.jpg are actual images in the directory)
        $orange = imagecolorallocate($im, 220, 210, 60);
        $px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
        imagestring($im, 3, $px, 9, $string, $orange);
        imagepng($im);
        imagedestroy($im);

?>
 

And that is a way of making a dynamic image. I have provided a relatively basic example of how to use dynamic images, I hope it was interesting :)

Feel free to ask me any questions!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Jan 042008
 

One of the major labels in the music industry (Sony BMG) has decided to remove the DRM protection on their MP3s.
Sony BMG is the last of the major record labels to cave in to the removal of DRM.

It is predicted that this will put a strain on Apple’s position in the music industry as we all know that iTunes relies heavily on the DRM protection it uses on every song downloaded.

Amazon is now likely to take some of Apple’s market as they’re in the process of releasing DRM-Free music, something that is much more appealing to any music lover.

Apple of course, has the iPod to fall back on. With the iPod being the most popular MP3 player at the moment which is tied to iTunes, maybe they will keep a firm grasp on the music industry after all….

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Jan 022008
 

Many people consider Thomas Edison to be one of the worlds greatest inventors of all time. With 1,093 US patents to his name you’d be right in thinking that. However Nikola Tesla was in a league of his own.
Nikola Tesla has around 700 patents awarded to him, but his inventions were revolutionary for the 20th century. He is sometimes considered to be the “man who invented the twentieth century” because his inventions have such a profound affect on our lives today.
Watch the videos below to see what I mean, they’re very interesting. (try to ignore the screaming woman in the intro to the programme lol)
YouTube Preview Image
YouTube Preview Image
YouTube Preview Image
YouTube Preview Image

Enjoy :D

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Jan 022008
 

I'm in the coding mood today :) .
I cannot count how many times I've been asked to code a UBER simple banner rotator script.
These things are so simple, but if you don't know PHP it can be difficult to understand.

The script below has two variables:

$links = array(
"http://www.google.com",
"http://www.msn.com",
"http://forums.digitalpoint.com"
);
//add a new link for the banner to a new line in the same format as above
 

and:

$images = array(
"http://www.google.co.uk/intl/en_uk/images/logo.gif",
"http://stc.msn.com/br/hp/en-us/css/35/decoration/msn_b.gif",
"http://forums.digitalpoint.com/images/misc/dps_logo.gif"
);
//add a new image link for the banner to a new line in the same format as above
 

As you can see they match up. The first line for the $links variable is for a google image, the second is for a msn image etc.
Here is the full code:

<?php
session_start();

$links = array(
"http://www.google.com",
"http://www.msn.com",
"http://forums.digitalpoint.com"
);
//add a new link for the banner to a new line in the same format as above

$images = array(
"http://www.google.co.uk/intl/en_uk/images/logo.gif",
"http://stc.msn.com/br/hp/en-us/css/35/decoration/msn_b.gif",
"http://forums.digitalpoint.com/images/misc/dps_logo.gif"
);
//add a new image link for the banner to a new line in the same format as above
//-------- DO NOT EDIT BELOW THIS LINE----------
$count = count($links) -1;

$randnum = mt_rand(0,$count);

if      ($randnum == $_SESSION['randnum'])
        {
        $randnum = mt_rand(0,$count);
        }
$_SESSION['randnum'] = $randnum;
echo '<a href="'.$links[$randnum].'"><img style="border:none" src="'.$images[$randnum].'" /></a>';

?>
 

The code has been constructed so that the likelihood of getting the same banner displayed twice is greatly reduced.

Comment here if you like the script, or have any questions :D

You can see an example of the script running here.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]