Method 1: Using print_r

Use For: Printing human-readable information about arrays and objects.

print_r is used to print human-readable information about a variable. It is particularly useful for arrays and objects.

Attempting to include file:

echo nl2br(htmlentities(print_r($data, true)));

Example 1:

$data = ['name' => 'John Doe', 'email' => 'john@example.com', 'age' => 30];

Result:

Array
(
    [name] => John Doe
    [email] => john@example.com
    [age] => 30
)

Example 2:

$data = [1, 2, 3, 4, 5];

Result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example 3:

$data = (object)['id' => 1, 'title' => 'Debugging'];

Result:

stdClass Object
(
    [id] => 1
    [title] => Debugging
)

Method 2: Using var_dump

Use For: Displaying detailed information about variables, including type and length.

var_dump displays structured information about one or more variables including its type and value. It provides more detailed information than print_r, including type and length for strings.

Attempting to include file:

ob_start();
var_dump($data);
$result = ob_get_clean();
echo nl2br(htmlentities($result));
    

Example 1:

$data = ['status' => true, 'count' => 5, 'items' => [1, 2, 3]];

Result:

array(3) {
  ["status"]=>
  bool(true)
  ["count"]=>
  int(5)
  ["items"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
}

Example 2:

$data = "Hello, World!";

Result:

string(13) "Hello, World!"

Example 3:

$data = 3.14159;

Result:

float(3.14159)

Method 3: Using sprintf

Use For: Creating formatted strings with variable values.

sprintf returns a formatted string. It is useful for creating strings with variable values inserted in specific formats.

Attempting to include file:

echo sprintf('User: %s, Age: %d', nl2br(htmlentities($user['name'])), nl2br(htmlentities($user['age'])));

Example 1:

$user = ['name' => 'Jane Doe', 'age' => 28];

Result:

User: Jane Doe, Age: 28

Example 2:

$price = 9.99;
echo sprintf('Price: $%.2f', $price);

Result:

Price: $9.99

Example 3:

$total = 12345;
echo sprintf('Total: %05d', $total);

Result:

Total: 12345

Method 4: Using printf

Use For: Directly outputting formatted strings.

printf outputs a formatted string. It works similarly to sprintf but outputs the string directly.

Attempting to include file:

printf('Product: %s, Price: $%.2f', nl2br(htmlentities($product['name'])), nl2br(htmlentities($product['price'])));

Example 1:

$product = ['name' => 'Laptop', 'price' => 799.99];

Result:

Product: Laptop, Price: $799.99

Example 2:

$quantity = 3;
$price = 19.95;
printf('Total cost for %d items: $%.2f', $quantity, $quantity * $price);

Result:

Total cost for 3 items: $59.85

Example 3:

$username = 'admin';
$loginTime = '10:30 AM';
printf('User %s logged in at %s', $username, $loginTime);

Result:

User admin logged in at 10:30 AM

Method 5: Using json_encode

Use For: Converting PHP arrays and objects into JSON format.

json_encode returns the JSON representation of a value. It is useful for converting PHP arrays and objects into a JSON format that can be used in JavaScript or stored in JSON files.

Attempting to include file:

echo nl2br(htmlentities(json_encode($data)));

Example 1:

$data = ['title' => 'Book', 'author' => 'Author Name', 'year' => 2021];

Result:

{"title":"Book","author":"Author Name","year":2021}

Example 2:

$data = ['apple', 'banana', 'cherry'];

Result:

["apple","banana","cherry"]

Example 3:

$data = (object)['foo' => 'bar', 'baz' => 'qux'];

Result:

{"foo":"bar","baz":"qux"}

Method 6: Using a Custom Debug Function

Use For: Creating reusable and customizable debug output.

A custom debug function allows for reusable and customizable debug output. It can encapsulate the behavior of print_r or var_dump, or any other debugging logic you want to include.

Attempting to include file:

function debugOutput($variable) {
    echo nl2br(htmlentities(print_r($variable, true)));
}
debugOutput($data);
    

Example 1:

$data = ['error' => false, 'message' => 'Operation successful'];

Result:

Array
(
    [error] => false
    [message] => Operation successful
)

Example 2:

$data = ['foo' => 'bar', 'baz' => 'qux'];

Result:

Array
(
    [foo] => bar
    [baz] => qux
)

Example 3:

$data = ['status' => 'success', 'code' => 200];

Result:

Array
(
    [status] => success
    [code] => 200
)

Method 7: Using a Simple Echo

Use For: Outputting simple strings and variable values.

echo is used to output one or more strings. It is the simplest way to output data in PHP.

Attempting to include file:

echo 'The value is: ' . htmlentities($value);

Example 1:

$value = 'Simple string';

Result:

The value is: Simple string

Example 2:

$num = 42;
echo 'The number is: ' . $num;

Result:

The number is: 42

Example 3:

$message = "Hello, world!";
echo htmlentities($message);

Result:

Hello, world!

Method 8: Using error_log

Use For: Logging errors and debug information without displaying them to the user.

error_log sends an error message to the web server's error log or to a file. It is useful for logging errors and other debug information without displaying them to the user.

Attempting to include file:

error_log(print_r($data, true));

Example 1:

$data = ['debug' => 'This is a debug message'];

Result:

[Logged in server error log]

Example 2:

$message = 'An error occurred';
error_log($message);

Result:

[Logged in server error log]

Example 3:

$user = ['name' => 'Alice', 'email' => 'alice@example.com'];
error_log(print_r($user, true));

Result:

[Logged in server error log]

Method 9: Using var_export

Use For: Getting a parsable string representation of a variable.

var_export returns a parsable string representation of a variable. It is similar to var_dump but the output is valid PHP code.

Attempting to include file:

echo nl2br(htmlentities(var_export($data, true)));

Example 1:

$data = ['key' => 'value'];

Result:

array (
    'key' => 'value',
)

Example 2:

$data = [1, 2, 3, 'foo' => 'bar'];

Result:

array (
    0 => 1,
    1 => 2,
    2 => 3,
    'foo' => 'bar',
)

Example 3:

$data = (object)['name' => 'John', 'age' => 25];

Result:

(object) array(
    'name' => 'John',
    'age' => 25,
)

Method 10: Using var_export with return

Use For: Storing the parsable string representation of a variable in another variable.

var_export with the return parameter set to true returns the variable's representation instead of outputting it directly. This can be useful for storing the representation in a variable.

Attempting to include file:

echo nl2br(htmlentities(var_export($data, true)));

Example 1:

$data = ['key' => 'value', 'numbers' => [1, 2, 3]];

Result:

array (
    'key' => 'value',
    'numbers' =>
    array (
        0 => 1,
        1 => 2,
        2 => 3,
    ),
)

Example 2:

$data = ['foo' => 'bar', 'baz' => [1, 2, 3]];

Result:

array (
    'foo' => 'bar',
    'baz' =>
    array (
        0 => 1,
        1 => 2,
        2 => 3,
    ),
)

Example 3:

$data = ['name' => 'Alice', 'attributes' => ['height' => 5.5, 'weight' => 140]];

Result:

array (
    'name' => 'Alice',
    'attributes' =>
    array (
        'height' => 5.5,
        'weight' => 140,
    ),
)

Method 11: Using debug_backtrace

Use For: Generating a backtrace that shows the call stack at the point where it's called.

debug_backtrace generates a backtrace that shows the call stack at the point where it's called. It is useful for understanding the sequence of function calls leading up to a point in your code.

Attempting to include file:

echo nl2br(htmlentities(print_r(debug_backtrace(), true)));

Example 1:

function test() {
    debugOutput();
}

function debugOutput() {
    echo nl2br(htmlentities(print_r(debug_backtrace(), true)));
}

test();
    

Result:

Array
(
    [0] => Array
        (
            [file] => /path/to/file.php
            [line] => 6
            [function] => debugOutput
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /path/to/file.php
            [line] => 10
            [function] => test
            [args] => Array
                (
                )

        )

)
    

Example 2:

function a() {
    b();
}

function b() {
    c();
}

function c() {
    echo nl2br(htmlentities(print_r(debug_backtrace(), true)));
}

a();
    

Result:

Array
(
    [0] => Array
        (
            [file] => /path/to/file.php
            [line] => 14
            [function] => c
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /path/to/file.php
            [line] => 10
            [function] => b
            [args] => Array
                (
                )

        )

    [2] => Array
        (
            [file] => /path/to/file.php
            [line] => 6
            [function] => a
            [args] => Array
                (
                )

        )

)
    

Example 3:

function first() {
    second();
}

function second() {
    third();
}

function third() {
    echo nl2br(htmlentities(print_r(debug_backtrace(), true)));
}

first();
    

Result:

Array
(
    [0] => Array
        (
            [file] => /path/to/file.php
            [line] => 14
            [function] => third
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /path/to/file.php
            [line] => 10
            [function] => second
            [args] => Array
                (
                )

        )

    [2] => Array
        (
            [file] => /path/to/file.php
            [line] => 6
            [function] => first
            [args] => Array
                (
                )

        )

)
    

Method 12: Using get_defined_vars

Use For: Getting an array of all defined variables in the current scope.

get_defined_vars returns an array of all defined variables in the scope where it is called. It is useful for debugging the current state of all variables.

Attempting to include file:

echo nl2br(htmlentities(print_r(get_defined_vars(), true)));

Example 1:

$var1 = "Hello";
$var2 = "World";
echo nl2br(htmlentities(print_r(get_defined_vars(), true)));
    

Result:

Array
(
    [_GET] => Array
    (
    )
    [_POST] => Array
    (
    )
    [_COOKIE] => Array
    (
    )
    [_FILES] => Array
    (
    )
    [_ENV] => Array
    (
    )
    [_SERVER] => Array
    (
    )
    [GLOBALS] => Array
    (
    )
    [var1] => Hello
    [var2] => World
)

Example 2:

$name = "Alice";
$age = 30;
echo nl2br(htmlentities(print_r(get_defined_vars(), true)));
    

Result:

Array
(
    [_GET] => Array
    (
    )
    [_POST] => Array
    (
    )
    [_COOKIE] => Array
    (
    )
    [_FILES] => Array
    (
    )
    [_ENV] => Array
    (
    )
    [_SERVER] => Array
    (
    )
    [GLOBALS] => Array
    (
    )
    [name] => Alice
    [age] => 30
)

Example 3:

$color = "blue";
$size = "large";
echo nl2br(htmlentities(print_r(get_defined_vars(), true)));
    

Result:

Array
(
    [_GET] => Array
    (
    )
    [_POST] => Array
    (
    )
    [_COOKIE] => Array
    (
    )
    [_FILES] => Array
    (
    )
    [_ENV] => Array
    (
    )
    [_SERVER] => Array
    (
    )
    [GLOBALS] => Array
    (
    )
    [color] => blue
    [size] => large
)

Method 13: Using Xdebug

Use For: Advanced debugging and profiling with features like stack traces and stepping through code.

Xdebug is a PHP extension that provides debugging and profiling capabilities. It offers features like stack traces, variable dumps, and the ability to step through your code. Xdebug is more advanced and requires setup and an IDE that supports it.

Installation:

sudo apt-get install php-xdebug

Configuration (add to php.ini):

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
    

Usage:

Use an IDE like PhpStorm, Visual Studio Code, or NetBeans which supports Xdebug. Enable profiling and analyze the results using tools like KCacheGrind.

Example 1:

function divide($a, $b) {
    return $a / $b;
}

$x = 10;
$y = 2;
$result = divide($x, $y); // Set a breakpoint here
    

Example 2:

function factorial($n) {
    if ($n <= 1) return 1;
    return $n * factorial($n - 1);
}

$number = 5;
$result = factorial($number); // Set a breakpoint here
    

Example 3:

function greet($name) {
    return "Hello, " . $name;
}

$name = "Bob";
$message = greet($name); // Set a breakpoint here