[Snippet] Función DEBUG

990 visitas 5 respuestas

Buenas de nuevo, viendo el post de Javier he buscado alguna función más que hay por ahí, y esta la verdad es de las que más utilizo.

La función en sí es muy simple, y muy mejorable, hay hasta clases que se ocupan de las tareas de Debug, pero a mi me ha gustado mantenerla lo más simple posible.

Esta función muestra los tipos de datos más comunes y sus datos asociados, muy útil para ver la salida de variables, constantes, etc..

function debug($var = null, $exit = FALSE)
{
    echo '<div id="debug" style="background:white;color:black;"><br /><pre>';

    if (is_array($var))
    {
        echo htmlentities(print_r($var, TRUE));
    }
    elseif (is_string($var))
    {
        echo "string(" . strlen($var) . ") \"" . htmlentities($var) . "\"\n";
    }
    else
    {
        var_dump($var);
    }

    echo '</pre></div>';

    if ($exit)
    {
        exit;
    }
}

por desde España

Registrado desde: 07 Jan 03

Respuestas

0 0

Buen aporte Mike.

Se me ocurre una mejora, podrías incluir la opción de devolver en una variable el resultado del debug, por si hiciera falta para enviar algún informe por correo o algo similar.

por desde España

Registrado desde: 02 Jul 02
1 0

Buenas!! ¿Te refieres a algo asi?

        function debug($var = NULL, $exit = FALSE, $returnMode = FALSE)
        {
            if(!is_bool($exit)) return FALSE;
            if(!is_bool($returnMode)) return FALSE;

            $return = NULL;

            $return .= '<div id="debug" style="background:white;color:black;"><br /><pre>';

            if (is_array($var))
            {
                $return .= utf8_decode(htmlentities(print_r($var, TRUE)));
            }
            elseif (is_string($var))
            {
                $return .= "string(" . strlen($var) . ") \"" . utf8_decode(htmlentities($var)) . "\"\n";
            }
            else
            {
                $return .= utf8_decode(print_r($var, TRUE));
            }

            $return .= '</pre></div>';

            if($returnMode === TRUE)
            {
                return $return;
            }
            else
            {
                echo $return;

                if($exit === TRUE) exit;
            }
        }

por desde España

Registrado desde: 07 Jan 03
0 0

No la he probado, pero tengo que reconocer que ahora me gusta más ;)

por desde España

Registrado desde: 02 Jul 02
0 0

Y bueno.. por dar alguna idea más para que la gente modifique a su gusto, por ejemplo una salida coloreada, faltaría pulir, pero para que se viera la idea:

function debug($var = NULL, $exit = FALSE, $returnMode = FALSE)
        {
            if(!is_bool($exit)) return FALSE;
            if(!is_bool($returnMode)) return FALSE;

            $return = NULL;

            $return .= '<div id="debug" style="background:white;color:'. ini_get('highlight.string') .';"><br /><pre>';

            if (is_array($var))
            {
                $return .= htmlentities(print_r($var, TRUE));
            }
            elseif (is_string($var))
            {
                $return .= "string(" . strlen($var) . ") \"" . htmlentities($var) . "\"\n";
            }
            else
            {
                $return .= print_r($var, TRUE);
            }

            $return .= '</pre></div>';

            $return = str_replace( '[', '</span>[<span style="color:'. ini_get('highlight.default') .'">', $return);
            $return = str_replace( ']', '</span>]', $return);
            $return = str_replace( '=>', '<span style="color:'. ini_get('highlight.keyword') .'">=></span>', $return);
            $return = str_replace( '(', '<span style="color:'. ini_get('highlight.keyword') .'">(</span>', $return);
            $return = str_replace( ')', '<span style="color:'. ini_get('highlight.keyword') .'">)</span>', $return);

            if($returnMode === TRUE)
            {
                return $return;
            }
            else
            {                
                echo $return;

                if($exit === TRUE) exit;
            }
        }

Un saludo!!!;)

por desde España

Registrado desde: 07 Jan 03
2 0

Una pequeña modificación, estaba trabajando con un theme de bootstrap y tuve problemas con las visibilidades del div de debug, asi que le añadí un par de propiedades para que estuviera siempre visible:

function debug($var = NULL, $exit = FALSE, $returnMode = FALSE)
        {
            if(!is_bool($exit)) return FALSE;
            if(!is_bool($returnMode)) return FALSE;

            $return = NULL;

            $return .= '<div id="debug" style="background:white;color:'. ini_get('highlight.string') .';z-index:99999;position:relative;"><br /><pre>';

            if (is_array($var))
            {
                $return .= htmlentities(print_r($var, TRUE));
            }
            elseif (is_string($var))
            {
                $return .= "string(" . strlen($var) . ") \"" . htmlentities($var) . "\"\n";
            }
            else
            {
                $return .= print_r($var, TRUE);
            }

            $return .= '</pre></div>';

            $return = str_replace( '[', '</span>[<span style="color:'. ini_get('highlight.default') .'">', $return);
            $return = str_replace( ']', '</span>]', $return);
            $return = str_replace( '=>', '<span style="color:'. ini_get('highlight.keyword') .'">=></span>', $return);
            $return = str_replace( '(', '<span style="color:'. ini_get('highlight.keyword') .'">(</span>', $return);
            $return = str_replace( ')', '<span style="color:'. ini_get('highlight.keyword') .'">)</span>', $return);

            if($returnMode === TRUE)
            {
                return $return;
            }
            else
            {                
                echo $return;

                if($exit === TRUE) exit;
            }
        }

En concreto en la línea que abre el div añadí estas propiedades: z-index:99999;position:relative;

Un saludo!!!

por desde España

Registrado desde: 07 Jan 03