The method of PHP debug via the Chrome browser console (console) _php tips

Source: Internet
Author: User
Tags php server php debug

The effect of the following figure

The PHP console is a chrome plug-in that can help users simulate the actual PHP Web environment and help users debug PHP code using the Chrome plugin, and after the user has installed the PHP console plugin in Chrome, You can reference the PHP console project code in your own PHP project and output debugging information for the PHP console plug-in to capture, so that after the site is published successfully, you can also use the PHP console plug-in to output debugging information to the Chrome browser, The PHP console plug-in allows users to view these debug logs from the PHP server.

When our PHP program is debug, the more common way is to print the variable information to the browser, for example:

<?php
echo ' <pre> ';
Print_r ($bar);
Echo ' </pre> ';
Exit

But printing directly may interfere with your page, disrupt the layout, and affect the data returned as the API and other issues. This is the simplest example of how to export debug information to a file:

Error_log (Print_r ($bar, true));

But this way is used to record the log is also appropriate, used to debug after all is not very convenient. At this point we can use the console of modern browsers, such as the Chrome console (win shortcut ctrl+shift+j), to output content to the console, you can solve this problem.

Now there are some tools such as php-console or Chrome Logger, the former for PHP, which supports a variety of service-side languages, by installing a Chrome extension first and then providing a server-side library for invocation. The implementation method is basically to use session,cookies to pass data.

As above is a relatively perfect solution, all provide a rich function. But if you just want a simple, usable solution to this problem without installing an extender, then you can do this:

<?php
function Console_log ($data)
{
	if Is_array ($data) | | is_object ($DATA))
	{
		echo ("< Script>console.log (' "Json_encode ($data)."); </script> ");
	}
	else
	{
		echo (' <script>console.log ('. $data. '); </script> ");
	}

The essence is to add a JavaScript script in the page, using the Console.log () function output information to the console, such as the method printed out is a string form, if the single quotes removed, the PHP array and objects will be in the way of the JS object printed, that is, Echo (" <script>console.log (". Json_encode ($data)."); </script> "), which is the way to see what you like.
As above is the simplest way to achieve, but doing so is not very good, is the output is very messy, if you call the function in different positions, then the page will be inserted in each position a <script>, although JS in the page almost anywhere can, but we can have a better way To unify all data to one output:

<?php
//Register a shutdown function, if not so remember at the end of the program Echo Console_log::fetch_output ();
Register_shutdown_function (' My_shutdown ');

function My_shutdown ()
{
	echo console_log::fetch_output ();
}

Class Console_log {
  private static $output = ';
  static function log ($data)
  {
    if (Is_array ($data) | | is_object ($DATA))
    {
      $data = Json_encode ($data );
    }
    Ob_start ();
    ? >
		<?php if (self:: $output = = = "):?>
		<script>
		<?php endif;? >
		console.log (' <?= $data;? > ');
    <?php
    self:: $output. = Ob_get_contents ();
    Ob_end_clean ();
  }
  static function Fetch_output ()
  {
  	if (self:: $output!== ')
  	{
  		self:: $output. = ' </script> '; c30/>} return
    self:: $output;
  }
}

Note that using <?= instead of <?php echo, you need to open short_open_tag=on in php.ini. This code is to use PHP's output control buffer function to save the data, the last time all the output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.