Created
September 9, 2017 17:33
-
-
Save phpfiddle/87c9cf863132ee77b3aa518a6e09b850 to your computer and use it in GitHub Desktop.
[ Posted by Alfertson Cedano ] PHP tratamiento de nulos, vacíos, en blanco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$arrVacio=array(); | |
$varDeclarada; | |
$arrValores=array( | |
'vacio ("")'=>'', | |
'en blanco (" ")'=>' ', | |
'NULL'=>NULL, | |
'TRUE'=>TRUE, | |
'FALSE'=>FALSE, | |
'0 (como cadena)'=>'0', | |
'0 (como entero)'=>0, | |
'0.0 (como float)'=>0.0, | |
'arr'=>$arrVacio, | |
'Declarada pero sin valor'=>$varDeclarada, | |
'No declarada'=>$strWhat, | |
'byte null \0'=>'\ 0' | |
); | |
//$var=""; | |
$strCSS=" | |
<style> | |
table { | |
border-collapse: collapse; | |
} | |
td, th { | |
border: 1px solid #999; | |
padding: 0.5rem; | |
text-align: left; | |
} | |
</style>"; | |
echo $strCSS; | |
//$strHTML=$strCSS; | |
$strHTML= "<table>"; | |
$strHTML.="<thead>"; | |
$strHTML.="<tr>"; | |
$strHTML.= "<th>Valor</th><th>Tipo</th> | |
<th>isset(var)</th> | |
<th>empty(var)</th> | |
<th>is_null(var)</th>; | |
<th>if(var)</th>"; | |
$strHTML.="</tr>"; | |
$strHTML.="</thead>"; | |
$strHTML.="<tbody>"; | |
$str=""; | |
//echo probarNulos($arrValores, $str); | |
$strDatos= probarNulos($arrValores,$str); | |
$strHTML.=$strDatos; | |
$strHTML.= "</tbody>"; | |
$strHTML.= "</table>"; | |
echo $strHTML; | |
function probarNulos($arrValores,$str){ | |
foreach ($arrValores as $k=>$v){ | |
$str.="<tr>"; | |
$str.="<td>"; | |
$str.=$k; | |
$str.="</td>"; | |
$str.="<td>"; | |
$str.=gettype($v); | |
$str.="</td>"; | |
$str.="<td>"; | |
$str.=var_export(isset($v),true); | |
$str.="</td>"; | |
$str.="<td>"; | |
$str.=var_export(empty($v),true); | |
$str.="</td>"; | |
$str.="<td>"; | |
$str.=var_export( is_null($v),true); | |
$str.="</td>"; | |
$str.="<td>"; | |
$str.= $v ? 'true': 'false'; | |
$str.="</td>"; | |
$str.= "</tr>"; | |
} | |
return $str; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment