php time类 Exception类

<?php
echo time();


//输出当前日期


//时区问题 G
//1.页面设置
date_default_timezone_set("PRC");
//2.php.ini设置


echo date("Y-m-d G:i:s");
?>

节约判断代码 方便维护

<?php
	
	try{
		addUser("aa");
		updateuser("xx");	
	}catch(Exception $e){
		echo "error = ".$e->getMessage();
	}	
	function addUser($username){
		if($username=="aa"){
			//ok
		}else{
			throw new Exception("add error");
		}
	}
	function updateuser($username){
		if($username=="aa"){
			//ok
		}else{
			throw new Exception("update error");
		}
	}

?>

顶级异常处理

<?php
	
	function my_exception($e){
		echo "我是顶级异常处理".$e->getMessage();
	}
	//修改默认的顶级处理器函数 (在抛出前设置)
	set_exception_handler("my_exception");

	function a1($val){
		if($val=="hello"){
			throw new Exception ("not input hello");
		}
	}
	try{
		a1("hello");	
	}catch(Exception $e){
		//获取异常
		//echo $e->getMessage();
		//可以继续抛出,这时将会启动php默认的异常处理器来处理
		//你可以自己定义一个顶级异常处理
		throw $e;
	}
	

?>





你可能感兴趣的:(php time类 Exception类)