Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active October 22, 2023 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/c431edd7fd914dae5bbaef49c5439648 to your computer and use it in GitHub Desktop.
Save code-boxx/c431edd7fd914dae5bbaef49c5439648 to your computer and use it in GitHub Desktop.
Debug PHP Code - Examples

HOW TO DEBUG PHP CODE EXAMPLES

https://code-boxx.com/how-to-debug-php-code/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) ERROR REPORTING LEVEL
// https://www.php.net/manual/en/errorfunc.constants.php
error_reporting(E_ALL & ~E_NOTICE); // ALL EXCEPT NOTICES
// error_reporting(E_ALL); // ALL KINDS OF ERROR
// error_reporting(0); // NO ERROR REPORTING
// (B) ERROR LOG
ini_set("log_errors", 1); // SAVE ERROR TO LOG FILE
ini_set("error_log", __DIR__ . DIRECTORY_SEPARATOR . "error.log"); // LOG FILE
// (C) DISPLAY ERROR MESSAGES
ini_set("display_errors", 1);
<?php
$foobar = doesnotexist();
require "doesnotexist.php";
<?php
$foobar = "Doge Bork"
$hello = "World";
$arr = ["Foo", "Bar'};
<?php
function addition ($x, $y) {
return $x + $y;
}
echo addition(1, "x");
<?php
if ($_POST["foo"]) {
echo "YES!";
}
<?php
function add ($x, $y) {
// Original intention
// return $x + $y;
// Someone did a *small* typo mistake
return $x - $y;
}
echo add(88, 22);
// Should be 110, but result is 66
<?php
// (A) START SESSION
session_start();
// (B) LET'S SAY WE HAVE A SHOPPING CART
$_SESSION["cart"] = [
"123" => [
"name" => "Foo bar",
"qty" => 99
],
"234" => [
"name" => "Doge",
"qty" => 88
]
];
// (C) WHAT'S IN THE CART?
print_r($_SESSION);
var_dump($_SESSION);
<?php
require "second.php";
$testObj = new Test();
$testObj->foo();
<?php
class Test {
function foo () {
var_dump(debug_backtrace());
debug_print_backtrace();
}
}
<?php
// (A) INIT
$varA = ["Hello", "World", "Foo", "Bar"];
$varB = "";
// (B) CONCAT
foreach ($varA as $txt) { $varB .= $txt; }
// (C) DISPLAY & STOP HERE
print_r($varA);
echo $varB;
exit();
// (D) MORE PROCESSING
$varA[] = "Doge";
$varB .= "Last";
<?php
// (A) CURL INIT
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://DOES-NOT-EXIST.com/dummy.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false
]);
// (B) CURL FETCH
$result = curl_exec($curl);
if ($result === false) {
error_log("Failed connecting to https://DOES-NOT-EXIST.com", 0);
}
curl_close($curl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment