donglei7152
2018-08-24 18:42
浏览 102
已采纳

如何在php中检查控制台上的变量值?

I want to check variable value in console I have tried everything but I am not getting the value in console.

 // Here I want to check $cart_id value before if Statement
 echo "Cart Id Before If Statement:" . $cart_id;

The above line is not working because the page add_cart skip and I am not able to check the value like I usually do. So I need to check this value on cosole like

console.log($cart_id);

But when I check console the console is showing empty. But When I click on network then I open preserve log then my file add_cart.php is showing and I am able to see cookie as well as form data but I have no idea how to check $cart_id in console or in any way.

I also tried in my helper.php class

function debug_to_console( $data ) {
$output = $data;
if ( is_array( $output ) )
    $output = implode( ',', $output);

echo "<script>console.log( 'Debug Objects: " . $output . "' ); 
</script>";
} 

And call it before my if statement

debug_to_console($cart_id); 

But above also not working for me. I am not able to check the result of $cart_id before if statement. Nothing show on console.

if ($cart_id != '') {

}else{

$items_json = json_encode($item);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}') ");
$cart_id = $db->insert_id;
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}

Your valuable suggestion would be welcome.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzheng9221 2018-08-24 19:53
    已采纳

    If its only to debug your application code, you can simply add a dump in your php code before if statement, like -

    var_dump($cart_id);die();
    
    if ($cart_id != '') {...}
    

    As, the call is ajax, you will have an http call generated in network tab. Opening that select Response. There you will be able to see the value of $cart_id. That can be found as shown in below image -

    enter image description here

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题