[eluser]TheFuzzy0ne[/eluser]
Hi everyone. I thought I'd shared these functions with you, in case anyone finds them useful. I use them constantly myself.
As you all know, functions are fantastic for saving your time and typing on tasks that are repeated over and over again. I often find that even the simplest piece of code becomes tedious after you've had to type it several hundred times in an hour, more so when it involves characters that aren't what I'd consider standard (which is almost any key that's not alphanumeric, with the exception of a few). I believe the following can help make your code cleaner (which is my personal opinion), but I also find I make less mistakes. I'm terrible when it comes to typos, because my fingers seem to be about as agile as a bus.
If it's just me who finds such aliases useful, please let me know so I don't keep posting code which no-one will ever use.
Function 1:
This function takes 1 or 2 arguments. It's only really useful when you provide the second argument. If the first parameter is FALSE, NULL, '', 0 etc, the second parameter is returned.
Code:
function if_or($arg1, $arg2='')
{
return ($arg1) ? $arg1 : $arg2;
}
Function 2:
The function below is similar to the function above, only it checks if the first parameter is set. If it's not, the second parameter is returned.
Code:
function if_isset_or($arg1, $arg2='')
{
return (isset($arg1)) ? $arg1 : $arg2;
}
Function 3:
This function is arguably the most useful of the three. I tend to spend a lot of time using print_r to dump arrays onto the page for debugging. I find it annoying having to constantly type the code below, perhaps it's just me, but typing all of that over and over again drives me nuts.
Simply feed it an array as a parameter, and place the output wherever you want to.
Code:
function pre_print_r($array)
{
return '<pre>' . print_r($array, TRUE) . '</pre>';
}
I hope this proves useful to one person at least.
Thanks.