Un programmatore sa quanto può essere utile effettuare il debug di un programma per capire se funziona correttamente. A tal proposito ho costruito una semplice funzione PHP che scrive su un file di testo il valore della variabile che vogliamo tracciare
function myDebug($var){ // File debug name $file = "myDebug.txt"; // Open file debug if (!(file_exists($file))) {fopen($file,"w");} // Variable name foreach($GLOBALS as $var_name => $value) { if ($value === $var) { $variableName = $var_name; } } // Debug Var $stringDebug = "-------------------------------------------------------------"."\n"; $txtDebug=fopen($file,"a+"); fwrite($txtDebug,$stringDebug); $stringDebug = "Var: ".$variableName."\n"; $txtDebug=fopen($file,"a+"); fwrite($txtDebug,$stringDebug); $stringDebug ="Value: "; if (gettype($var) === 'array'){ ob_start(); var_dump($var); $content = ob_get_contents(); ob_end_clean(); $stringDebug .= "\n".$content."\n"; } else{ $stringDebug .= $var."\n"; } $txtDebug=fopen($file,"a+"); fwrite($txtDebug,$stringDebug); // Close Debug fclose($txtDebug); }
Supponi di voler tracciare nella tua routine PHP le variabili $a, $b, $c
$a = array("apple", "beer"); $b = "Hello"; $c = 44; myDebug($a); myDebug($b); myDebug($c);
Quando esegui la routine verrà creato il file "myDebug.txt" con questo contenuto: