1

I have been working in Javascript and JSON Objects for some time and find the JS objects are easy to see what is inside; you can log it to the console; structure it and basically see many levels deep. which is great for working in objects I didnt make so I can see what is in there and what I have access to.

to pretty print JSON I usually use something like this:

var str = JSON.stringify(obj, null, 2); // spacing level = 2
console.log('my pretty JSON Object: ', str );

How would I take an object like $myObject in PHP and output (print_r?) it? I don't really care if it is to the console; I really just want to know how PHP developers look inside of and navigate PHP objects.

I know I can json convert it and print it to the console; thats all fine and dandy but is that what people do on a regular basis? I figured there would be a way to do this without requiring another language

Eolis
  • 645
  • 2
  • 9
  • 22

3 Answers3

2

There are a number of debugging packages available. However, if you just want something simple you can implement for yourself, you could write the data to a log file. Assuming, you want to do this in many locations you should create a class and call the class each time you want to debug your code. You may also want to keep track of the date and time of when the logging occurred.

Here is an example that you can use and customize it to your own needs:

Create a file called Debug.php:

<?php
class Debug {

    public static function output($module, $text){
        $date = date("l jS \of F Y h:i:s A");
        file_put_contents ("/opt/logs/myProject.log", "================\n", FILE_APPEND);
        file_put_contents ("/opt/logs/myProject.log", "$date\n", FILE_APPEND);
        file_put_contents ("/opt/logs/myProject.log", "$module: $text\n", FILE_APPEND);

    }
}

Then in any of your other php files you can include the Debug class with the following line:

require_once("Debug.php");

Then wherever you want to use it just run the following command:

Debug::output("My Module #1", "This is a test");

OR

Debug::output("My Module #1 - My Variable", $my_variable);

OR

Debug::output("My Module #2 - My Object", json_encode($my_object));

OR

Debug::output(__FILE__, json_encode($my_object));

*Note the double underscores on either side of FILE. Or use one of the other server variables for getting the appropriate file name.

kojow7
  • 10,308
  • 17
  • 80
  • 135
1

You can't output from php directly to browser console, because php runs on the server, while your console is on the client. I don't think it's a good idea to output php debug information into the console, but if you absolutely must, you can do output some javascript form php to log your object into console:

$myObject = new MyObject();
$myEscapedJson = str_replace('"','\"', json_encode($myObject));
...
echo "<script>console.log(\"My object from PHP: $myEscapedJson\");</script>
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Alright; I know I can do that, but is there no way to look inside the object? How do people use PHP and traverse objects on a regular basis? there must be a way to see what they contain aside from JS converting and logging – Eolis May 19 '16 at 16:02
  • Use xdebug and step through the code from an IDE. Or at least use xdebug extension and `print_r`. – Aleks G May 19 '16 at 16:03
-1

coming back to this, this is the code I regularly use these days when I want to see whats up from my php in the console for testing:

            ?>
            <script>
              $( document ).ready(function($){
                var json_test  = <?php echo json_encode( $your_php_variable, JSON_FORCE_OBJECT ) ?>;
                console.log("im testing testy: ", json_test );
              });// document.ready END
            </script>
            <?php
Eolis
  • 645
  • 2
  • 9
  • 22