0

I'm experienced in Javascript, the way i debug now is to console.log(vars), so the way i debug is oberserving my vars in the program, if they contain sth.,an object etc. what sometime also helps it's the error log in the browser, but this is more a side effect, this approach works for me in javascript, but since a few months i'm trying to debug PHP with it, and made little to no progress, it seems quite difficult to set up PHP for this, and there are log-files in general ...

Question: Is watching the values of variables not the way how you debug PHP? and if not, what approach is the right one?

1
  • You can use var_dump($your_var) in php for oberserving variables values Commented Aug 17, 2021 at 23:36

2 Answers 2

2

There are tools like xDebug that can help debugging PHP, although I've never had a lot of luck with it beyond its profiling capabilities.

Place echo and var_dump() statements in appropriate places in the code and watch the output.

You can include error_log() statements to log details to a dedicated log file that you can examine.

And there's the server error log.

That's what I use - others may have more.

Sign up to request clarification or add additional context in comments.

2 Comments

And the xDebug is only way to debug. All other ways such var_dump(), dd(), etc - is not debug.
@Maksim you must have a frightfully narrow definition of the term 'debug'
2

If you want to debug variable content in php there are several ways to do it You could use var_dump () To fully show the content of the variable (object, class or whatever this variable is). If you want to debug for errors you can active the error display config on .htaccess, .user.ini or using php comands to active the error display options.

E.g. If you are using a local PHP server, in most cases the error display options are active for default.

So If you just want to debug a variable conten, like $_POST, you can use

 var_dump($_POST);

You cant prevent error using isset() for variables that you dont know if have any value.

if ( isset($_POST) ) var_dump($variable_name);

you can try this code

URL: http://localhost/vardump.php?test=this%20is%20a%20variable%20content%20test&value=123

echo 'This is a $_POST debug';
var_dump( $_POST );

echo 'This is a $_GET debug'; 
var_dump( $_GET );

echo 'This is a non setted variable';
var_dump( $variable_name );

result

    This is a $_POST debug

D:\wamp64\www\vardump.php:4:
array (size=0)
  empty

This is a $_GET debug

D:\wamp64\www\vardump.php:7:
array (size=2)
  'test' => string 'this is a variable content test' (length=31)
  'value' => string '123' (length=3)

This is a non setted variable
( ! ) Notice: Undefined variable: variable_name in D:\wamp64\www\vardump.php on line 10
Call Stack
#   Time    Memory  Function    Location
1   0.0002  407224  {main}( )   ...\vardump.php:0

D:\wamp64\www\vardump.php:10:null

This is an example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.