Monday, January 10, 2011

PHP Floating Point Bug Crashes Servers

"A newly unearthed bug in certain versions of the PHP scripting language could crash servers when the software is given the task of converting a large floating point number, raising the possibility that the glitch could be exploited by hackers. The bug will cause the PHP processing software to enter an infinite loop when it tries to convert the series of digits "2.2250738585072011e-308" from the string format into the floating point format. The bug only seems to affect version 5.2 and 5.3 of the language.""Computer scientist Rick Regan first reported the bug on Monday, and the PHP development team issued patches the following day."


Thursday, January 6, 2011

Time spent on writing Unit Tests

Many times, developers spend more time writing unit tests than writing actual code.

Code that could have been shipped long ago, is still stuck because of the unit tests.

How much time do you spend writing unit tests?

See Joel Spolsky & Jeff Atwood's good discussion on Test Driven Development:  http://www.joelonsoftware.com/items/2009/01/31.html

Unit Test - Good or Bad?

TDD (Test Driven Development) has been pretty prevailing in the development circles for the past decade or so.

The idea for sure is a noble and important idea - do not release code without testing it, and always have a set of tests ready - unit tests - to be run after every code change. If the test breaks - that's supposed to mean that you've broken the logic of the existing code.

"Supposed to mean..."

I just spent an hour on fixing a series of test that I broke, only to find out that what really broke is some side non important flow control... It had nothing to do with the function's functionality, other than the fact that since this control broke - the function wasn't pulling out any data (from the mock data set, which is material for another post...)
(OK, you might say it was a poorly designed unit test to begin with, but I suppose that's what happens when developers work under pressure; They must provide unit tests, and under a time constraint).

Yes, TDD is great, but sometimes completely misses the point.

Like anything good - don't exaggerate and don't overdo it.



Monday, August 31, 2009

php command line arguments

One of the cool things about PHP is the abundance of ready made built-in functions.

I write a lot of PHP shell scripts.

PHP, like almost any other proper language, accepts arguments in a $argv array, where the 1st argument ($argv[0]) is the filename, and then all of the rest of the arguments.

But what happens if you don't always want to accept the parameters as a command-line argument?

Then you either have to pass a predefined 'placeholder' that nominates 'use default value', or you have to define flags, and build a parsing mechanism to cut off the first character or two, and then define what to do with the rest.

This is where getopt() comes in handy.

All you have to do is define a. the different options the script can accept. b. if the option is required and/or accompanied with an additional argument.

example:

$options = getopt("ab:c:C::");

a is a standalone - no additional argument required (obviously - not required).
b: is required.
so is c: .
C:: (Capital C) is optional. (PHP 5.3 and up).

$options is an (associative) array of all passed parameters (with arguments, if applies).

Now that's effective.

Thursday, August 6, 2009

neatly print out PHP arrays

Use this function to neatly print out your arrays;

function print_r_html($data,$return_data=false) {

$data = print_r($data,true);

$data = str_replace( " "," ", $data);

$data = str_replace( "\r\n","
\r\n"
, $data);

$data = str_replace( "\r","
\r"
, $data);

$data = str_replace( "\n","
\n"
, $data);


if (!$return_data)

echo $data;

else

return $data;

}


displaying php source file


Ever wonder how to take a PHP file, and display it color coded in a web browser?

Well... Wonder no more!

PHP CLI (Command Line Interpreter) comes with this nifty feature that does this automatically for you.

php -s filename.php > filename.html

creates a html output of the php file.

Cool, ain't it?