Home >Backend Development >PHP Tutorial >Four PHP application small functions_PHP tutorial
1. How to view variable content
There is a var_dump($var) function in php4, which is used to dump the content of a variable (various types of variables), but the output of this function is not suitable for display in html pages. . You can write a function yourself as follows:
function dump( $var ) {
echo "
";
var_dump( $var );
echo "
";
}
Very suitable for debugging!
In php3, you can only dump the contents of an array recursively, but there is nothing you can do about objects. It seems like the previous article The Dump_Array function is such a function. :)
2. Static variables
function test()
{
static $s_val;
$s_val+=2 ;
return $s_val;
}
echo test();//2
echo test();//4
?>
3. A way to avoid the same name appearing multiple times
#----------------avoid the same name-------------
for($checkname=0;$checkname{
if ($name[$checkname]==$name[$num])
{
$num--;
break;
}
}
4. Get the extension of a file
Use this small function:
function fileextname ($filename)
{
$retval="";
$pt=strrpos($filename, ".");
if ($pt) $retval=substr($filename, $ pt+1, strlen($filename) - $pt);
return ($retval);