PHP: Problems with Long Integers & Scientific Notation

Posted by Jetlogs @ 11:44 am
Category: PHP,Web Development

For those of you working on PHP scripts using very large integers, a heed of warning. It seems that the current version of PHP (5.2.5) apparently has problems working with outputting very large integers and floats that have a lot of lagging 0′s

For example, here is the bug that I have encountered in a server using CentOS:

$big_integer = 1202400000;
echo $big_integer; //outputs 1.2024E+09

$sample_float = (float)100000; //float
echo $sample_float; //outputs 1E+05

As you can see, PHP will automatically convert these types of integers into scientific notation. At first, I though it was simply a case of integer overflow with 32 bit systems, or a misconfiguration in float precision. However, I’ve noticed that this bug will only apply to large integers or floats with a lot of zeros at the end. Apparently, when PHP encounters a number that exceeds the upper limit of 2,147,483,647 for an integer, it automatically converts the number’s type from integer into a double.

Through searching PHP’s bug archives, its seems that I am not alone. Apparently this bug has been documented in the recent versions and is most notable with systems running under CentOS.

So how do we fix this error? Fortunately, we can format these numbers in scientific notation back to their standard integer form using the number_format() function. Here is how to do it:

$big_integer = 1202400000;
$formatted_int = number_format($big_integer, 0, '.', '');
echo $formatted_int; //outputs 1202400000 as expected

$sample_float = (float)100000 //float;
$formatted_float = number_format($sample_float, 2, '.', '');
echo $formatted_float; //outputs 100000.00 as expected

Hope this helps anybody who is experiencing the same problems.


Auto-saving with jQuery

Posted by Jetlogs @ 2:22 am
Category: jQuery,PHP,Web Development

Auto-saving has been one of the great features that AJAX has given to us. Ever had a time when you were writing a very long article and suddenly, your browser crashes. Thanks to applications that implement auto-saving, data is less prone from lost due to these cases.

Ever wondered how auto-saving is done? Basically, it is just a simple AJAX script running in the background at set intervals to save the data you are currently working on. Here is a simple tutorial on how to implement auto-saving using jQuery Read more »


PHP: Humor and SQL Injections

Posted by Jetlogs @ 11:46 pm
Category: Humor,PHP,Web Development

xkcd really got to me with this comic. It shows why you should always filter your inputs, especially when you’re going to use them in an SQL query.

click to view full comic at xkcd


PHP: SimpleXML XPaths and Namespaces

Posted by Jetlogs @ 7:36 pm
Category: PHP,Web Development

Ever since PHP version 5.2.0, XML parsing has been made so much easier with the new SimpleXMLElement->xpath() function. No longer do we have to parse an XML document by working our way through all the child node, we just have to know the XML element’s name

Assuming we have an XML string assigned to $xml_string

$xml_string='<?xml version="1.0" encoding="utf-8"?>
<a>
	<b>
		<c>
			<d>
				<e>Hello World</e>
			</d>
		</c>
	</b>
</a>';

$xml = new SimpleXMLElement($xml_string);

Here is how it was done before 5.2.0 to access the <e> element

echo $xml->b->c->d->e[0]; //prints Hello World

Now with the new SimpleXMLElement->xpath() function, here is how it is done:

$result = $xml->xpath('//e');
echo $result[0]; //prints Hello World

Read more »


PHP: How to Make Life Easier with References

Posted by Jetlogs @ 8:30 am
Category: PHP,Web Development

Don’t you just hate it when you have to write very long codes such as below?

$name = mysql_real_escape_string(strip_tags($name));
$email = mysql_real_escape_string(strip_tags($email));
$address = mysql_real_escape_string(strip_tags($address));

Even if you created a function to make coding easier, it can still be a little bit tedious:

function escape_mysql($string)
{
	return mysql_real_escape_string(strip_tags($string));
}

$name = escape_mysql($name);
$email = escape_mysql($email);
$address = escape_mysql($address);

I’ll demonstrate a simple technique to make your life easier by using PHP’s references. By passing variables by reference, we can get rid of the variable assignment on the function’s return value. Here is how if should look like in the end:

function escape_mysql(&$string)
{
	$string = mysql_real_escape_string(strip_tags($string));
}

escape_mysql($name);
escape_mysql($email);
escape_mysql($address);

By passing variables by reference in our escape_mysql() function, the variable assignment no longer becomes necessary since the function will do it instead. Now that is a lot more easier to code and understand isn’t it?


Next Page »
  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License