Came across a rather “interesting” peculiarity in PHP, which affects mathematical operations involving floating point numbers. Before getting into the details, let me introduce you to the problem I was trying to help a friend solve. The goal was to take an arbitrary floating point number, let’s say 1.841243, and convert it to a whole number where decimal points became part of the whole, so 1.8432432 would become 18432432. The first solution was a very simple scriptlet, which if C has taught us anything should’ve worked.
[php]
$a = 1.8432432;
while ((int)$a != $a) $a *= 10;
[/php]
This code relies on 2 premises, which are true in PHP:
1) Casting a float to an int, results in a drop of decimal points. (Ex. (int) 1.8432432 => 1)
2) By multiplying value by 10, all decimal places will eventually be gone and (float)$a will equal (int)$a.
While both premises are true, the above code does not work, in fact, it results an unterminated loop, YIKES!
Quick confirmation with C code, included below, re-affirms th...
Yesterday I've discovered an "interesting" implication of using user input inside var_export() and print_r() functions. To those who have never used the two, a brief overview of their functionality. The var_export() function takes a variable and represents the data found within as a valid PHP string. By default this string is dumped to screen, but if you want you can have it be returned as a string, by passing a 2nd optional parameter as boolean TRUE. For example if you wanted to put an array creation code into a file, you'd do something like this:
[php]
file_put_contents("my_file", var_export($array, 1));
[/php]
The print_r() function is similar in function, except the returned data is intended for debugging and not storage. As with var_export() by passing a 2nd optional parameter you can force the data to be returned as a string rather then dumped to screen.
Herein lies the problem, when it comes to storing the data, this is done by enabling output buffering of the content without a set buffer size limi...
Finally got a semi-stable internet connection, a pretty amazing thing during a convention of this size. So, while George is serenading the audience with the wonders of DBXML, I get to do a bit of blogging :-).
The conference so far is a great deal of fun, had a chance to meet with many developers from other non-PHP projects, which is quite neat as most conferences I have a chance to attend to be PHP specific. The brief talk on performance I gave yesterday went rather well, with probably one of the largest audiences I've had at a conference and seemed to have gone rather well. The slides, to those interested are now available online (just click "talks" link at the top of the page and scroll down).
While for me OSCON is almost over :-(, I got an early flight back to Toronto tomorrow the next two conferences are already in the works. I will be speaking at php|works Conference in Toronto around mid-September and I will be giving quite a few talks at International PHP Conference in Frankfurt in early Novembe...
Came across a rather interesting article by IBM on how to convert IE specific web apps to work with Mozilla. Aside from the obvious, by listing the different approaches each browser adopts it serves as an excellent resource for making a web app cross-browser complaint. I hope they will update article with the other two commonly (relatively) used browsers, Konqueror/Safari (KTHML Engine) and Opera.
With the release of PHP 5.1.0b3 the dev tree has been closed for new features, allowing only bug fixes to facilitate the stabilization of the code for the upcoming (don't ask me when) 5.1.0 stable. This however created a unfortunate situation where PHP does not have a development tree for feature enchantments, so any improvements remain lingering on developer's boxes until development tree is once again available...
In the past few weeks I've been doing some work, which involved using parse_url() and cURL extension quite a bit and in the process came up with few improvements.
parse_url() tweak [ patch ]
This patch adds a 2nd optional parameter to parse_url() which allows the function to return a particular URL component rather then an array. Ex:
[php]
$host = parse_url("http://www.ilia.ws/gallery/", PHP_URL_HOST);
echo $host; // will print www.ilia.ws
[/php]
cURL improvements [ patch ]
The patch does two things; first of all it allows the user to retrieve the request sent by cURL to the server. I...