我有一个php文件,它是wordpress插件的一部分。我需要调试一个问题。我想找出一个变量的值是什么。如何将变量的值打印到控制台?已建议使用echo或chrome或firefox扩展名。我无法获得回显以输出到控制台(echo “$variablename";),也无法使用firephp扩展名firefox。
要回答您的问题,您可以执行以下操作:
echo '<script>console.log("PHP error: ' . $error . '")</script>';
Run Code Online (Sandbox Code Playgroud)
但我建议改用@Ishas建议的方法之一。确保$error不包含任何可能使脚本混乱的内容。
在这里,您可以看到我在 WooCommerce 中调试优惠券逻辑时针对实际问题的解决方案。该解决方案仅用于调试目的。(注:截图不是最新的,它也会暴露私人成员。)
\nfunction console_log(): string {\n list( , $caller ) = debug_backtrace( false );\n $action = current_action();\n $encoded_args = [];\n foreach ( func_get_args() as $arg ) try {\n if ( is_object( $arg ) ) {\n $extract_props = function( $obj ) use ( &$extract_props ): array {\n $members = [];\n $class = get_class( $obj );\n foreach ( ( new ReflectionClass( $class ) )->getProperties() as $prop ) {\n $prop->setAccessible( true );\n $name = $prop->getName();\n if ( isset( $obj->{$name} ) ) {\n $value = $prop->getValue( $obj );\n if ( is_array( $value ) ) {\n $members[$name] = [];\n foreach ( $value as $item ) {\n if ( is_object( $item ) ) {\n $itemArray = $extract_props( $item );\n $members[$name][] = $itemArray;\n } else {\n $members[$name][] = $item;\n }\n }\n } else if ( is_object( $value ) ) {\n $members[$name] = $extract_props( $value );\n } else $members[$name] = $value;\n }\n }\n return $members;\n };\n\n $encoded_args[] = json_encode( $extract_props( $arg ) );\n } else {\n $encoded_args[] = json_encode( $arg );\n }\n } catch ( Exception $ex ) {\n $encoded_args[] = \'`\' . print_r( $arg, true ) . \'`\';\n }\n $msg = \'``, `\'\n . ( array_key_exists( \'class\', $caller ) ? $caller[\'class\'] : "\\x3croot\\x3e" )\n . \'\\\\\\\\\'\n . $caller[\'function\'] . \'()`, \'\n . ( strlen( $action ) > 0 ? \'``, `\' . $action . \'`, \' : \'\' )\n . \'` \xe2\x9e\xa1\xef\xb8\x8f `, \' . implode( \', \', $encoded_args );\n $html = \'<script type="text/javascript">console.log(\' . $msg . \')</script>\';\n add_action( \'wp_enqueue_scripts\', function() use ( $html ) {\n echo $html;\n } );\n add_action( \'admin_enqueue_scripts\', function() use ( $html ) {\n echo $html;\n } );\n error_log( $msg );\n return $html;\n}\nRun Code Online (Sandbox Code Playgroud)\n// ...\n\ndefine( \'WP_DEBUG\', true );\n\n// ...\n\n/** Include WP debug helper */\nif ( defined( \'WP_DEBUG\' ) && WP_DEBUG && file_exists( ABSPATH . \'wp-debug.php\' ) ) {\n include_once ABSPATH . \'wp-debug.php\';\n}\nif ( ! function_exists( \'console_log\' ) ) {\n function console_log() {\n }\n}\n\n/** Sets up WordPress vars and included files. */\nrequire_once( ABSPATH . \'wp-settings.php\' );\nRun Code Online (Sandbox Code Playgroud)\n<head>在渲染HTML 之前:console_log( $myObj, $myArray, 123, "test" );\nRun Code Online (Sandbox Code Playgroud)\n<head>渲染后(在模板等中/当上述不起作用时使用):echo console_log( $myObj, $myArray, 123, "test" );\nRun Code Online (Sandbox Code Playgroud)\n <caller class>\\<caller function>() <caller action/hook> \xe2\x9e\xa1\xef\xb8\x8f <variables ...>\nRun Code Online (Sandbox Code Playgroud)\n| 归档时间: |
|
| 查看次数: |
9239 次 |
| 最近记录: |