Welcome Guest, Not a member yet? Register   Sign In
Three simple, but useful helper functions
#1

[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.
#2

[eluser]Pascal Kriete[/eluser]
The first two are a little too general for my taste, but number 3 is one I also use a lot. (I use a textmate snippet, but it serves the same purpose).
#3

[eluser]Flyingbeyond[/eluser]
yeah, i agree,the function 1 and 2 perform great,especially in my latest project ...

PS: it seems i am quite familiar with TheFuzzy0ne's signature ..er..freeweb7 ? is that ok? Smile
#4

[eluser]TheFuzzy0ne[/eluser]
Hi!

Sorry, I'm not quite sure what you mean about my signature. Would you please explain?
#5

[eluser]Flyingbeyond[/eluser]
[quote author="TheFuzzy0ne" date="1223166412"]Hi!

Sorry, I'm not quite sure what you mean about my signature. Would you please explain?[/quote]

well, i once met a moderate in a forum who has the same signature with yours , therefore , i thought you were him ... just a mistake ...
#6

[eluser]sankai[/eluser]
Code:
function pre_print_r($array)
{
    return '<pre>' . print_r($array, TRUE) . '</pre>';
}

I think the function name is too long...
Maybe name 'print_p' is better.
#7

[eluser]John_Betong[/eluser]
[quote author="sankai" date="1227990064"]
Code:
function pre_print_r($array)
{
    return '<pre>' . print_r($array, TRUE) . '</pre>';
}

I think the function name is too long...
Maybe name 'print_p' is better.[/quote]
&nbsp;
I name that function fred(...) Smile
&nbsp;

Code:
//==========================================================================  
function fred($msg=array('one', 'two', 'three'), $msg_name='$msg_name not passed')
{
  echo "<pre style='text-align:left; font-size:1.0em; background:#ffc none; text-align:left'>";
    echo '<b>' .$msg_name .'</b>';
    echo '<br />';
    print_r($msg);
   echo "</pre>";
    // return '';
}//endfunc

&nbsp;
&nbsp;
It is located in my localhost index.php so it is available everywhere regardless.
&nbsp;
&nbsp;
#8

[eluser]sankai[/eluser]
nice idea,get highlighter print_r area for debug output
#9

[eluser]Thorpe Obazee[/eluser]
Nice idea. Haven't actually written a helper for myself when I encounter any of the above situations. I'm sure I will have a use for it on my pet project.
#10

[eluser]obiron2[/eluser]
[quote author="Flyingbeyond" date="1223154588"]yeah, i agree,the function 1 and 2 perform great,especially in my latest project ...

PS: it seems i am quite familiar with TheFuzzy0ne's signature ..er..freeweb7 ? is that ok? Smile[/quote]

Erm - and me - lol




Theme © iAndrew 2016 - Forum software by © MyBB