PHP 简单自定义函数


<?

$b=90;

//自定义函数------------------------------------------------
function _ziding($val,$val2=1){  //创建自定义函数_ziding()  用来接受 输出端 传值
global $b;  //global全局变量用来直接取顶上 $B值  不加这个就无法取函数外的变量值

$a=$val+$val2+$b;               //定义变量$A
return $a;                  //把$a  的结果返回给输出端
}




//输出端-------------------------------------------------------
$a=_ziding(1,2);  //定义变量  把值传给自定义函数
echo  "$a<br> "; //输出自定义函数  最后输出为3




//判断自定义函数是否存在-----------------------------------------
if (function_exists(_ziding)){   //判断函数是否存在专用函数function_exists
echo "函数存在<br>" ;
}else{
echo "函数不存在" ;
}



//  不使用全局变量也能在 自定义函数中取外部的变量


//外部变量--------------------------------
$bb= "爸爸";         //自定义函数外的变量$bb
$mm= "妈妈";         //自定义函数外的变量$mm


//自定义函数-------------------------------------------------------------------------------------
function _ziding2($bb,$mm,&$j){ //接受输出端传来的参数 &$j 这个代表接受和返回结果 没这个输出端就取不到$J
//global $bb,$mm;
$j=$bb."和".$mm;               //定义变量 $j
echo "函数内输出:$j<br>";            //在函数内输出变量$j
}



//输出端--------------------------------------------------------------
_ziding2($bb,$mm,$j);          //定义变量  把值传给自定义函数  当前$J位空值   
echo "输出端输出:$j<br>";          //在输出端输出变量$j   这里能取到返回的值全靠 &



/*
输出为

函数内输出:爸爸和妈妈
输出端输出:爸爸和妈妈

*/

/* //
  * 将字节转换成Kb或者Mb
  * 参数 $num为字节大小
  */

//自定义字节转换函数   (转KB 和MB)---------------------------------------------------------------------------
$num=12424233;    //定义变量  要被转换的字节

//自定义函数开始
function bitsize($num){           //定义函数bitsize  $num用来接收 输出端的值    
  if(!preg_match("/^[0-9]+$/", $num)) return 0;
  //如果不存在数字 则返回0
  echo $num > 1024 ? ($num/1024 > 1024 ? round($num/1024/1024, 2)." Mb" : round($num/1024, 2)." Kb") : $num." 字节";
 //否则函数内输出  如果大于1024 则  $num除1024>1024 则按MB处理  多除一个1024         : 否则就按KB处理         : 否则直接输出字节(如果不大于1024
 
}


//输出端
bitsize($num);    //把获取的内容传递给 函数
//echo bitsize($num);  在输出端输出

//输出为   11.85 Mb








echo "<br>"; 
/* /
  * 功能:综合提示JS代码输出
  * 参数 $msg 为提示信息
  *      $direct 为提示类型 0为提示(默认)1为提示刷新返回 2为提示返回
  * 输出提示代码并结束程序
  */
  
  
  //自定义函数--------------------------------------------------------------------
function alert_msg($msg, $direct = "0"){      //创建自定义函数   接收输出端传来的值
  switch($direct){                        //switch循环条件判断函数值
  
    case '0':                            //如果$direct=0 则输出如下
    $msg=无提示;                          //提示语
    $script = "";                        //地址为空
	break;                              //跳离循环
	
    case '1':                         //如果$direct=1  则输出如下
    $msg=返回到首页;                  //提示的文字
    $script = "location.href=\"".$_SERVER["HTTP_REFERER"]."\";";  //地址为当前目录
    break;                        //跳离循环  
	
    case '2':                   //如果$direct=2  则输出如下
    $msg=返回上页面;              //提示语
    $script = "history.back();";   //返回上页面 
    break;                      //跳离循环
	
   default:                  //如果条件都不符合   则输出如下
    $msg=返回到指定页面;       //提示语
    $script = "location.href=\"".$direct."\";";  // 输出什么页面就上什么页面  echo alert_msg($msg,"INDEX.PHP") 这样就可以跳转到INDEX.PHP
  }
  
  echo "<script language='javascript'>window.alert('".$msg."');".$script."</script>";
  exit;
}


//函数引用  输出端----------------------------------------------------
//echo alert_msg($msg,"0");


//输出结果
/*
JS提示  无提示
无跳转
*/


/* /////////////////////////////////////////////////////////////////
  * 功能:综合载入页面函数

  */
function gotourl($message='',$url='',$title='')
{

    $html ="<html><head>";
    if(!empty($url))
    $html .="<meta http-equiv='refresh' content=\"3;url='".$url."'\">";
    $html .="<link href='../templates/style.css' type=text/css rel=stylesheet>";
    $html .="</head><body><br><br><br><br>";
    $html .="<table cellspacing='0' cellpadding='0' border='1' width='450' align='center'>";
    $html .="<table border='1' cellspacing='1' cellpadding='4' width='100%'>";
    $html .="<tr class='m_title'>";
    $html .="<td>".$title."</td></tr>";
    $html .="<tr class='line_1'><td align='center' height='60'>";
    $html .="<br>".$message."<br><br>";
    if (!empty($url))
    $html .="系统将在3秒后返回<br>如果您的浏览器不能自动返回,请点击[<a href=".$url." target=_self>这里</a>]进入";
    else
    $html .="[<a href='#' οnclick='history.go(-1)'>返回</a>]";
    $html .="</td></tr></table></td></tr></table>";
$html .="</body></html>";
echo $html;
exit;
}

//输出
gotourl($message='111',$url='http://www.baidu.com',$title='正在进入你的页面')

?>

猜你喜欢

转载自blog.csdn.net/qq_40437676/article/details/108243535