Data Types in PHP

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 πŸ˜€

Posted in Coding | Tagged , , , , , , , | 2 Comments

How to make a Dynamic Image

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/wp-content/themes/papaface/images/beer.png" />

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


<img src="http://www.papaface.com/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/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[&apos;id&apos;]; //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`=&apos;".mysql_real_escape_string($linkid)."&apos;"); //@ 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!

Posted in Coding | Tagged , , , , | 3 Comments

The end of DMR coming soon?

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….

Posted in Miscellaneous | Tagged , , , , | Leave a comment

The greatest inventor of all time. Nikola Tesla

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 πŸ˜€

Posted in Miscellaneous | Tagged , , , , , , | Leave a comment

A Simple Banner Rotator Script

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[&apos;randnum&apos;])
	{
	$randnum = mt_rand(0,$count);
	}
$_SESSION[&apos;randnum&apos;] = $randnum;
echo &apos;<a href="&apos;.$links[$randnum].&apos;"><img style="border:none" src="&apos;.$images[$randnum].&apos;" /></a>&apos;;

?>

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 πŸ˜€

You can see an example of the script running here.

Posted in Coding | Tagged , , , | 13 Comments

Tools to getting started…

Getting started with what you say?

Getting started to your own blog…

Oh, that’s sounds awesome… wait… what is a blog?

A blog is a personal site that is dedicated to news or articles that are about one or many given subjects.

Now I’m interested, but what would my blog be about?

Well, first start off with your hobbies. Do you often play the Playstation 3 or your xbox 360? Maybe you like hunting, fishing, trap shooting? Most common blogs are about peoples life directly. What they did at school, or updates on their work activities. For instance, PapaFace.com is a blog directed in Web Design.

Ok, I figured out what I want my blog to be about… where do I start?

Well first, the easiest blogging system I have ever ran into was Google Blogs… called Blogger.

Blogger however limits your templating and coding spectre.

Ok, I’ve used Blogger, but as you said… I am limited on what I can do.

Ok, go ahead and grab a cheap little host.

As a customer of this host… http://www.aptohost.com/ I am obligated to recommend them! Sure the template is sloshy, but they are 100% a great host.

Then go ahead and zoom over to one of these sites (no selected order except for word press because it’s by far the best)

http://wordpress.org/ (The greatest blog tool ever)
http://www.blogphp.net/ (Kind of sloshy, but easy for custom upgrades)
http://www.kubelabs.com/kubeblog/ (Nice little blogging tool, very nice)
http://www.publicwarehouse.co.uk/php_scripts/lightblog.php (Again, sloshy, but easy for custom upgrades)

Ok, I’ve installed the one I chose. Loving it! Now how do I get more people to view my blog…

Ok, there is quite a few methods to gaining hits on your blog.

1) If your signed up to a community forum, go ahead and leave your URL in your signature. Give it some color so it stands out.
2) Add it to some RSS feeds and blog directories according to your blog category.
3) Add some downloads that match up to your site, there is always some freeloaders and they love free stuff!
4) If you have some money, add some advertisements to frequently viewed websites.
5) Send a huge e-mail to everyone on your list explaining how cool your blog is and how it might give them cancer (don’t really say that, it’s not that funny) if they don’t visit your blog.

Thanks Justin, I want to send you some money for helping me.

I can’t accept any donations of any sort, I love helping people. Just send me a link to your blog!

Posted in Miscellaneous | Leave a comment

Dutch plan to build new island

The Dutch are a crazy bunch, but I didn’t think they were this crazy. Okay, so the country is getting a bit crowded and because of global warming the sea is rising and eating away at the coastline, but what do you do? You could encourage people not to do the ‘business’ so the population will steady off a little. Or you could build sea defences. But building a whole new island? I understand why they want to do it. The new island will create lots of new room for people to live. It will also act as a shield to protect the actual country itself. But here is the interesting bit, they have decided to make it in the shape of a tulip!

It may seem a bit strange, but the Dutch know their stuff when it comes to water and land. The USA asked for some advice when Hurricane Katrina hit in 2005, the Dutch have also helped India and China with their water problems.

Some people think that they are making the island into a recognised shape for publicity, and attention. I can image there will be some property developers eagerly watching this!

Posted in Miscellaneous | Leave a comment

Confused Deer

Part of me wants to keep sorry for this obviously confused animal, but half of me want to join in with the fun. The video shows a deer that has been caught up in a swing. It looks like its antlers have got caught, and it is trying to get free. I wonder how long it was there swinging around?

YouTube Preview Image

Posted in Miscellaneous | Leave a comment

What people in 1961 thought life in 2000 would be like!

Every now and again I think about what it would be like to live 20, 40 even 100 years ago. Sometimes when the dinosaurs were around, but they didn’t have broadband so I’m not that interested!

In 1961 a few scientist predicted what life would be like in 2000. It looked as though everything would be so easy that people will probably die from sheer boredom. Here’s what they thought:

Doors will open automatically, and clothing will be put away by remote control. The heating and cooling systems will be built into the furniture and rugs.

You’ll have a home control room – an electronics centre, where messages will be recorded when you’re away from home. This will play back when you return, and also give you up-to-the minute world news, and transcribe your latest mail.

You’ll have wall-to-wall global TV, an indoor swimming pool, TV-telephones and room-to-room TV. Press a button and you can change the décor of a room.

The status symbol of the year 2000 will be the home computer help, which will help mother tend the children, cook the meals and issue reminders of appointments.

Cooking will be in solar ovens with microwave controls. Garbage will be refrigerated, and pressed into fertiliser pellets.

Food won’t be very different from 1961, but there will be a few new dishes – instant bread, sugar made from sawdust, foodless foods (minus nutritional properties), juice powders and synthetic tea and cocoa. Energy will come in tablet form.

At work, Dad will operate on a 24 hour week. The office will be air-conditioned with stimulating scents and extra oxygen – to give a physical and psychological lift.

Mail and newspapers will be reproduced instantly anywhere in the world by facsimile.

There will be machines doing the work of clerks, shorthand writers and translators. Machines will “talk” to each other.

It will be the age of press-button transportation. Rocket belts will increase a man’s stride to 30 feet, and bus-type helicopters will travel along crowded air skyways. There will be moving plastic-covered pavements, individual hoppicopters, and 200 m.p.h. monorail trains operating in all large cities.

The family car will be soundless, vibrationless and self-propelled thermostatically. The engine will be smaller than a typewriter. Cars will travel overland on an 18 inch air cushion.

Railways will have one central dispatcher, who will control a whole nation’s traffic. Jet trains will be guided by electronic brains.

For more, click here

Posted in Technology Stuff | 1 Comment