Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How to Debug php

When we write huge lines of code in PHP and then some error occurs then removing that error might be a heck of the task. Some basic errors that programmer do while programming in PHP which are:

to print or debug there should be error in php code

Image description

Image description

Missing Semicolon “;” and closing brackets “}”.
To debug the above errors, using a good PHP ide will be very helpful as it will suggest the closing bracket “}” and end of statement by “;”.
Misspelling a variable name. Remember $var != $Var as we know, PHP is a case sensitive language.
Using “=” instead of “==” (Assignment operator and Equal operator)
Example:

Image description

Error2

Image description

Image description

error 3
Image description

Image description

solution
Image description
Image description
Image description

** method2 **
Image description
Image description

Error6
Image description
Image description
Image description
Image description
Image description
Image description

How to check the values that are sent in a POST request in PHP

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Check if it's a POST request

    // Debug the entire $_POST array
    echo "Debugging the POST data: ";
    var_dump($_POST);

    // Alternatively, you can use print_r for a more human-readable output
    // print_r($_POST);
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

How to check the values that are sent in a POST request in PHP and display line by line

if(isset($_POST["admin_id"])){
    echo "Debugging the POST data: ";

    foreach ($_POST as $key => $value) {
        if (is_array($value)) {
            // If the value is an array, iterate through its elements
            echo "$key:<br>";
            foreach ($value as $item) {
                echo "- $item<br>";
            }
        } else {
            echo "$key: $value<br>";
        }
    }
Enter fullscreen mode Exit fullscreen mode

output

Image description

Top comments (0)