Converting a PHP string number into a numeric value

Despite seeming like a very basic procedure, this evening I found I had wasted half an hour, split between Googling for the answer and trying to work it out for myself.

I have a number that needs to be stored in a string format in PHP. However, I need to use this value at some point to run a calculation, for which I need the true value as opposed to the string. If I had just wanted a whole number the answer is very simple, use the PHP (int) method to convert it.

It is not that easy when you are working with decimals. Unless I am missing something (in which case feel free to embarass me in the comments below!) there is no easy way to do this. Here is what I came up with:

$string = "3.142";        // The source number, as a string
$a = explode(".",$string);            // Split the string, using the decimal point as separator
$int = $a[0];                        // The section before the decimal point
$dec = $a[1];                        // The section after the decimal point
$lengthofnum=strlen($dec);            // Get the num of characters after the decimal point
$divider="1";                        // This sets the divider at 1
while($i < ($lengthofnum)) {
$divider.="0";                    // Adds a zero to the divider for every char after the decimal point
$divider=(int)$divider;                // Converts the divider (currently a string) to an integer
$total=$int+($dec/$divider);        // compiles the total back as a numeric value

If anyone knows of a quicker / cleaner / better / more funky way of doing this simple task please do let me know… if not then I hope this little snippet at least lets you avoid losing the half our of your life that I lost this evening!

UPDATE: Thanks to Xobman, who Twittered me this little gem:

Try $foo = “3.123”; $bar = (float) $foo;

LOL… low and behold, it works! That’ll teach me to post my (oh so clever!) code on the Interweb for all to see 😀

Thanks Xobman!

Displaying a random image using PHP

Lyndi posted about displaying an image at random the other day. Her solution was to use javascript as it enabled the page to do this even if the server had caching on. It was a good solution, but it got me thinking about going in a slightly different direction…

The solution relied on the image names being hard coded into the script. I thought it would be quite cool to come up with a solution that simply looked in a directory for all the JPG’s or GIF’s, and plucked one at random.

This is what I came up with:

$dir = “images”; // The directory where your images are
$filetypes = (“jpg”||”gif”); // Whatever images are valid. You could add “png” etc

srand((double)microtime()*123456789); // Generates a random number seed
$count = 0; // Initialises the counter

$dirOpen = opendir($dir); // Opens the image directory

while(($im = readdir($dirOpen)))
if($im != “..” && $im != “.” && substr($im,-3)==$filetypes) // Disregards “..” and “.” (direcrory structure)
$image[$count] = $im; // Reads an image
$count++; // Increments the counter

closedir($dirOpen); // Closes the directory

$randname = rand(0,(count($image)-1)); // Generates the random number
echo ‘<img src=”‘.$dir.’/’.$image[$randname].'” alt=”Random Image” />’; // Publishes the image


I have commented the code, so it should be fairly self-explanatory how it works.

It is a bit rough and ready, but it does the job. To implement this on a website, simply use:

<? include(‘randimg.php’)?>

The beauty of this solution is you can add as many images as you like to the images (or whatever you call it) folder. Add and remove them at will and the script just picks from the images that are there at the time.

Let me know what you think.