PHP Exception catch Handling

 更新时间:2016年11月25日 16:01  点击:1700

例外是用来改变脚本的正常流动,如果指定的错误


-------------------------------------------------- ------------------------------

什么是异常
在PHP 5中来到一个新对象的处理错误导向的方式。

异常处理是用来改变代码的执行,如果指定的错误(例外的正常流动)条件发生。这种情况称为例外。

这就是通常发生异常时触发:

当前状态保存代码
该代码的执行将切换到预定的(自定义)异常处理函数
根据这一情况,处理便可恢复已保存的代码由国家执行,终止或继续执行脚本,从代码中的不同位置的脚本
我们将显示不同的错误处理方法:

基本使用异常
创建自定义的异常处理程序
多例外
重新抛出异常
设置顶层异常处理
注意:例外只应使用错误条件,而不应被用来在指定的跳点代码中的另一个地方。


-------------------------------------------------- ------------------------------

例外的基本用法
当抛出异常,下面的代码也不会执行,而PHP将试图找到匹配“catch”块。

如果一个异常没有被捕获,一个致命的错误会发出一个“未捕获的异常”消息。

让我们试着扔不抓出异常:

 

<?php
//create function with an exception
function checkNum($number)
  {
  if($number>1)
    {
    throw new Exception("Value must be 1 or below");
    }
  return true;
  }

//trigger exception
checkNum(2);
?>

Fatal error: Uncaught exception 'Exception'
with message 'Value must be 1 or below' in C:webfoldertest.php:6
Stack trace: #0 C:webfoldertest.php(12):
checkNum(28) #1 {main} thrown in C:webfoldertest.php on line 6

Try, throw and catch


为了避免从上面的例子中的错误,我们需要创造适当的代码来处理异常。

适当的异常代码应包括:

尝试 - 函数使用一个例外,应在一个“试”块。如果异常不会触发的代码将继续正常。但是,如果异常触发,一个异常“抛出”
扔 - 这是如何触发异常。每一个“扔”必须有至少一个“捕获”
捕获 - 阿“catch”块检索例外,创建一个对象,包含异常信息
让我们试着触发一个有效的代码除外

 <?php
//create function with an exception
function checkNum($number)
  {
  if($number>1)
    {
    throw new Exception("Value must be 1 or below");
    }
  return true;
  }

//trigger exception in a "try" block
try
  {
  checkNum(2);
  //If the exception is thrown, this text will not be shown
  echo 'If you see this, the number is 1 or below';
  }

//catch exception
catch(Exception $e)
  {
  echo 'Message: ' .$e->getMessage();
  }
?>

php switch 语法用于执行基于不同条件不同的行动。


-------------------------------------------------- ------------------------------

开关语句的PHP
使用switch语句来选择代码的许多组成部分之一被执行。

语法

switch (n)
{
case label1:
  code to be executed if n=label1;
  break;
case label2:
  code to be executed if n=label2;
  break;
default:
  code to be executed if n is different from both label1 and label2;
}

这是它的工作原理:首先,我们有一个单一表达式n(常常是一个变量),只计算一次。该表达式的值,然后比较与结构中每个案件的价值观。如果有匹配,与该案件相关的代码就会被执行。利用休息时间,避免因进入下一个代码自动执行。默认语句用于如果没有找到匹配。

例如

<html>
<body>

<?php
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>

switch (expression)
{
case label1:
  code to be executed if expression = label1;
  break; 
case label2:
  code to be executed if expression = label2;
  break;
default:
  code to be executed
  if expression is different
  from both label1 and label2;
}

</body>
</html>

工作原理:

对表达式(通常是变量)进行一次计算
把表达式的值与结构中 case 的值进行比较
如果存在匹配,则执行与 case 关联的代码
代码执行后,break 语句阻止代码跳入下一个 case 中继续执行
如果没有 case 为真,则使用 default 语句
<?php
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>

</body>
</html>

PHP stripos

stripos() 函数返回字符串在另一个字符串中第一次出现的位置。与strpos(),stripos()不区分大小写

string 必需。规定被搜索的字符串。
find 必需。规定要查找的字符。
start 可选。规定开始搜索的位置。

<?php
$findme    = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';

$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);

// Nope, 'a' is certainly not in 'xyz'
if ($pos1 === false) {
    echo "The string '$findme' was not found in the string '$mystring1'";
}

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' is the 0th (first) character.
if ($pos2 !== false) {
    echo "We found '$findme' in '$mystring2' at position $pos2";
}
?>

strpos() - 查找的字符串的第一个出现的位置
strrpos() - 查找字符串中的字符的最后出现的位置
strrchr() - 查找字符串中的字符最后一次出现
substr() - 返回字符串的一部分
stristr() - 忽略大小写strstr
strstr() - 查找第一次出现的字符串
strripos() - 找出案件的最后出现的位置在一个字符串不敏感的字符串
str_ireplace() - 病例的str_replace区分版本。

php strtolower字母转换小写

串用strtolower(string $str )
返回字符串转换为小写的所有字母字符。

请注意,'字母'是当前语言环境决定的。这意味着,即如元音变音默认的“C”语言环境,字符甲(一)将不转换

$str输入字符串。报告错误返回值返回小写的字符串。


<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so
?>
UTF 8使用mb_convert_case

exampel

<?php
$string = "А";
$string = mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
echo $string;

//output is: а
?>

php addslashes处理$_POST $_GET数组函数

这是我的一个相当于自动版本的功能,用于处理$ _POST数组有用

<?php
function add_slashes ($an_array) {
  foreach ($an_array as $key => $value) {
    $new_array[$key] = addslashes($an_array[$key]);
  }
}
?>

then call it:

<?php add_slashes($_POST); ?>

嗨,
我使用这个递归函数。它处理多维数组。


<?php
function as_array(&$arr_r)
{
 foreach ($arr_r as &$val) is_array($val) ? as_array($val):$val=addslashes($val);
 unset($val);
}

as_array($_POST);
?>

复杂一点的。

<?php
//create array to temporarily grab variables
$input_arr = array();
//grabs the $_POST variables and adds slashes
foreach ($_POST as $key => $input_arr) {
    if(is_array($input_arr)){       
        $_POST[$key] = addslashes_array($input_arr);
    }else{
        $_POST[$key] = addslashes($input_arr);
    }
   
}

// Recursive Function to add slashes with posted array.
function addslashes_array($input_arr){
    if(is_array($input_arr)){
        $tmp = array();
        foreach ($input_arr as $key1 => $val){
            $tmp[$key1] = addslashes_array($val);
        }
        return $tmp;
    }else{
        return addslashes($input_arr);
    }
}

?>

[!--infotagslink--]

相关文章