Okay, who hasn’t been here.
You need to test some code, so you echo out a variable.
Or worse yet, you have to echo out an array or object.
Doesn’t seem so bad, right? If you’ve done any testing, then you know you’ll be commenting out, deleting, rewriting, commenting out, deleting, and rewriting those lines over and over again. Tedius. Not fun.
So I’ve come up with my own testing function.
Its purpose and inputs needed are within the name itself:
- echo: outputs PHP variable to the screen.
- data: the minimum input needed to output the variable. (This can be a string, a number, an array, or an object. The function will determine what it is and output the correct HTML syntax.)
- title: this is an optional input for you to put the variable’s name.
- die: optional command to kill the code after outputting the variable. This can be ‘die’, ‘yes’, ‘y’, or 1 to kill the code. Any other value will allow the code to keep running.
echo_data_title_die($shopping_cart_object, 'shopping cart object', 'die');
Running the above code will output a string with a line break, a <pre> formatted array (so it is more readable), as well as a <pre> formatted object.
Another helpful everyday use:
It’s always a good thing to know what file and line number you are on when debugging.
The full code: