Methods of Debugging – PHP

|
| By Webner

Methods of Debugging by dumping information

These methods are used for debugging in PHP. We can see the variable values in human-readable form on the console and can find errors in case there are issues due to variables incorrect values. print_r() and var_dump() are used in core PHP as well as its Framework. dd() is newly introduced in Laravel.

  1. print_r():
    This command gives the human-readable information about a variable.
    Syntax-

    		$variable = Account::all();
                    print_r($variable);
                    die();
    

    In the above example, die() will stop executing a further line of code. This will give us structured output as shown below-

  2. var_dump():
    This command also gives readable information but in a nonstructured manner.
    Syntax-

    		$variable = Account::all();
    	        var_dump($variable);
                    die();
    

    Similarly, as print_r() example, this also gives output in the console and stops further execution. The output is not properly readable. Here is the output that you will see-

  3. dd():
    Here ‘dd’ stands for “Dump and Die”. This means it will give the variable value and stop executing further code. We do not have to explicitly write die() command.
    Syntax-

    	       $variable = Account::all();
                   dd($variable);
    

    This will also give us information in the form of collection and we have to open each sub-collections to see each value.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *