You may want to check out var_export — while it doesn't provide the same output as var_dump it does provide a second $return parameter which will cause it to return its output rather than print it:
$debug = var_export($my_var, true);
Why?
I prefer this one-liner to using ob_start and ob_get_clean(). I also find that the output is a little easier to read, since it's just PHP code.
The difference between var_dump and var_export is that var_export returns a "parsable string representation of a variable" while var_dump simply dumps information about a variable. What this means in practice is that var_export gives you valid PHP code (but may not give you quite as much information about the variable, especially if you're working with resources).
Demo:
$demo = array(
"bool" => false,
"int" => 1,
"float" => 3.14,
"string" => "hello world",
"array" => array(),
"object" => new stdClass(),
"resource" => tmpfile(),
"null" => null,
);
// var_export -- nice, one-liner
$debug_export = var_export($demo, true);
// var_dump
ob_start();
var_dump($demo);
$debug_dump = ob_get_clean();
// print_r -- included for completeness, though not recommended
$debug_printr = print_r($demo, true);
The difference in output:
var_export ($debug_export in above example):
array (
'bool' => false,
'int' => 1,
'float' => 3.1400000000000001,
'string' => 'hello world',
'array' =>
array (
),
'object' =>
stdClass::__set_state(array(
)),
'resource' => NULL, // Note that this resource pointer is now NULL
'null' => NULL,
)
var_dump ($debug_dump in above example):
array(8) {
["bool"]=>
bool(false)
["int"]=>
int(1)
["float"]=>
float(3.14)
["string"]=>
string(11) "hello world"
["array"]=>
array(0) {
}
["object"]=>
object(stdClass)#1 (0) {
}
["resource"]=>
resource(4) of type (stream)
["null"]=>
NULL
}
print_r ($debug_printr in above example):
Array
(
[bool] =>
[int] => 1
[float] => 3.14
[string] => hello world
[array] => Array
(
)
[object] => stdClass Object
(
)
[resource] => Resource id #4
[null] =>
)
Caveat: var_export does not handle circular references
If you're trying to dump a variable with circular references, calling var_export will result in a PHP warning:
$circular = array();
$circular['self'] =& $circular;
var_export($circular);
Results in:
Warning: var_export does not handle circular references in example.php on line 3
array (
'self' =>
array (
'self' => NULL,
),
)
Both var_dump and print_r, on the other hand, will output the string *RECURSION* when encountering circular references.
A CodeIgniter helper is a PHP file with multiple functions. It is not a class
Create a file and put the following code into it.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('test_method'))
{
function test_method($var = '')
{
return $var;
}
}
Save this to application/helpers/ . We shall call it "new_helper.php"
The first line exists to make sure the file cannot be included and ran from outside the CodeIgniter scope. Everything after this is self explanatory.
Using the Helper
This can be in your controller, model or view (not preferable)
$this->load->helper('new_helper');
echo test_method('Hello World');
If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file i.e. <your-web-app>\application\config\autoload.php.
$autoload['helper'] = array('new_helper');
-Mathew
Best Solution
Create a
helpers.phpfile in your app folder and load it up with composer:After adding that to your
composer.jsonfile, run the following command:If you don't like keeping your
helpers.phpfile in yourappdirectory (because it's not a PSR-4 namespaced class file), you can do what thelaravel.comwebsite does: store thehelpers.phpin the bootstrap directory. Remember to set it in yourcomposer.jsonfile:Tip: If you want to use the different file name instead of
helpers, you can change the file name and path. Also, you can create multiple helper files. It will look like this: