PHP 分岐処理、入力チェックなどメモ

Actionの分岐

switch ($action){
 
    case '1':
 
        break;
 
    case '2':
 
 
        break;
 
    case '3':
 
        break;
 
    default:
 
        break;
 
}

 

入力チェック

if(empty($action) || is_null($action)){
 
    //NULL、からチェック
    $action = 0;
 
}else if (!preg_match("/^[1-3]{1}$/i",$action)){
 
    //actionの数字1桁チェック
 
}
 
switch ($action){
 
    case '1':
 
        if(is_email(trim($mail))){
 
            return $error;
        }
        break;
 
    case '2':
 
        if(empty($mem_id) || is_null($mem_id)){
            return $error;
        }
 
        break;
 
    case '3':
 
        if(empty($mem_id) || is_null($mem_id)){
            return $error;
        }
 
        break;
 
    default:
 
        break;
 
}
 
 
function is_email($mail) {
    $ptn = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
    if(strstr($mail, '@') && strstr($mail, '.')) {
        if (preg_match($ptn, $mail)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

 

 

DBの条件

if($ws_param['action'] === '2' || $ws_param['action'] === '3'){
    $ws_temper = "inf.mem_id = :mem_id AND";
}

 

 

 

参考資料

http://antonsan.net/study/php/php014.php

http://www.gadgety.net/shin/tips/unix/regexp.html

http://bittorrentlove.blog58.fc2.com/blog-entry-107.html