There’s a very simple technique you can use to debug PHP code in web applications run with MAMP in your development environment: write data to the PHP error log. No fancy debuggers or anything, just plain output to the error log. This is how I do it.
First, open your terminal and run the following commands:
1. cd /Applications/MAMP/logs
2. tail -f php_error.log
This will show the last few messages printed in the error log. By running the tail command with the -f
flag, new output written to the error log will automatically be shown in your terminal.
Next, use the error_log
function in your code to output the variable you want to inspect. This is pretty simple when you want to print a variable with a scalar data type (boolean, integer, float, or string). For example, consider the following PHP code:
$name = "Tomas"; $age = 34; $likesDogs = true; $hasDog = false; $tau = 6.28; error_log("name: {$name}"); error_log("age: {$age}"); error_log("likes dogs: {$likesDogs}"); error_log("has dog: {$hasDog}"); error_log("tau: {$tau}");
This code will output the following to the error log:
[17-Nov-2018 06:02:23 America/Panama] name: Tomas [17-Nov-2018 06:02:23 America/Panama] age: 34 [17-Nov-2018 06:02:23 America/Panama] likes dogs: 1 [17-Nov-2018 06:02:23 America/Panama] has dog: [17-Nov-2018 06:02:23 America/Panama] tau: 6.28
Note the output for the boolean variables. $likeDogs
is set to true
, and its value is shown in the error log as 1
. However, $hasDog
is set to false
, and no value is actually written to the error log. This is also the case for variables set to null
.
Now, let’s consider a more complex data type such as an associative array:
$user = array( 'name' => 'Tomas', 'age' => 34, 'likesDogs' => true, 'hasDog' => false );
You might be tempted to simply use error_log
like you did earlier:
error_log("user: {$user}");
However, this will actually write a PHP notice to the error log:
[17-Nov-2018 06:14:40 America/Panama] PHP Notice: Array to string conversion in /Applications/MAMP/htdocs/caveman-debugging-sample/index.php on line 23
Not exactly what you were expecting. To actually see the contents of the $user
array, you can use either print_r
or var_export
. Use error_log
with print_r
as like this:
error_log("user: " . print_r($user, true));
This will print the following on the error log:
[17-Nov-2018 06:19:51 America/Panama] user: Array ( [name] => Tomas [age] => 34 [likesDogs] => 1 [hasDog] => )
Or use error_log
with var_export
like this:
error_log("user: " . var_export($user, true));
which will print the following on the error log:
[17-Nov-2018 06:29:53 America/Panama] user: array ( 'name' => 'Tomas', 'age' => 34, 'likesDogs' => true, 'hasDog' => false, )
Note that none of these methods actually print out any data types. To get data types printed, you can use good old var_dump
combined with ob_start
, ob_get_contents
, and ob_end_clean
to buffer var_dump’s output and prevent it from going directly to the browser:
ob_start(); var_dump($user); $output = ob_get_contents(); ob_end_clean(); error_log("user: {$output}");
This is basically what print_r
and var_export
do behind the scenes, but the output you get in the error log is slightly different:
[17-Nov-2018 06:43:55 America/Panama] user: array(4) { ["name"]=> string(5) "Tomas" ["age"]=> int(34) ["likesDogs"]=> bool(true) ["hasDog"]=> bool(false) }
And that’s it! Simple caveman-style PHP debugging using the MAMP error logs. No fancy tools with breakpoints or stepping through lines of code, yet still pretty useful.