I have found many times when writing stuff in PHP that I have come across a need to generate a random number. That’s not too bad in and of itself, but what about when you need a unique ID key or something like that. For example, say you are writing the next best helpdesk application and need unique case numbers and don’t want to simply use a counter. For whatever reason, I put this little function together and it has served me well. Check it out …
Archive for the ‘Codebank’ Category
29
Generate Random Unique Number
23
PERL Trim Functions
PERL is a wonderful scripting language, it is extremely powerful, flexible and portable. It also lacks a couple basic functions that other languages have built in. Fear not my friend, just like the PERL round function, I have functions for other things as well!
One thing I miss is a trim function. They have chop and chomp, but that doesn’t always fit what I want. Below I have included a few suns that will get the job done nicely, check it out. Read the rest of this entry »
16
PERL Round Function
Hey all your PERL junkies like me, I have a present for you. Anyone that has done any coding at all other PERL will know (and miss) the round function that most other languages have built in. For those that may not know, the round function lets you do just that, round a number to the specified digit. So, instead of having to use 3.14159265 as an answer for a particular equation, you could round it to 3.14. Nice, huh?
Well, here is a neato little round function that you can drop into your PERL scripts and call to actually round numbers instead of cutting them off with ceil or cut. Check it out:
sub round { my($number) = shift; return int($number + .5 * ($number <=> 0)); }
There you go!
**Update**
Thanks to Thierry H. for adding a little mod to the round function allowing you to specify the number of decimals to print. Here is the modified function:
sub round { my $number = shift || 0; my $dec = 10 ** (shift || 0); return int( $dec * $number + .5 * ($number <=> 0)) / $dec; }
You would call it by giving not only the number to round, but how many numbers to show on the right side of the decimal. It will look like so:
$result = round(123.4567,3);
This should return 123.457 (the 6 in the third slot gets rounded to 7). There ya go, thanks for the mod Thierry! You can check out Thierry’s site here.
26
MySQL Error Handler For PHP
Anyone that works with MySQL and PHP knows that it’s a good idea to trap and handle errors as a part of making calls to the database. I have seen folk that do it in many different ways, and some that don’t do it at all. It’s a real handy thing to do, especially in the early phases of development. If there is a problem somewhere in your code, good feedback from the application can make troubleshooting much easier. I tend to write functions that make life easier, put them all into a file and then reference that from my pages that actually do the work. In this case, I have a function that will catch and return MySQL errors, along with the query so you can see what is going wrong. First I will show you the function, and then I will show you some usage examples.


