php actual combat common code example

1 outputs an array in sequence. Enter a new line for every 3 entered.

 //Tell me a little detail. Some people say that $i< sizeof($len) needs to calculate the length of the array every time it loops, whether it is a waste of performance. -----Answer: 1 You observe very carefully. 2 The current interpreter is more intelligent, and this problem will be automatically optimized.

for($i  = 0; $i<sizeof($len); $i++){  
   if( $i && $i%3==0) echo "<br>";
   echo $arr[$i];
}

 2 Strings do not allow special characters.

        $chinese = "\x{4e00}-\x{9fa5}"; // Chinese match 
        $pattern ="/^[a-zA-Z0-9( $chinese )]+$/u"; // add English, number match 
        if (! preg_match ( $pattern , $nick_name )) {
                 exit ("No special characters allowed" );
        }

3 Pay for the strlen and substr functions in utf-8. (find the length of the utf-8 string, find the substring of utf-8) 

   // utf-8 find utf-8 string length 
   function   strlen_utf8( $str )
   {
        $i = 0;
        $count = 0;
        $len = strlen($str);
        while ($i < $len) {
            $chr = ord($str[$i]);
            $count++;
            $i++;
            if ($i >= $len)
                break;
            if ($chr & 0x80) {
                $chr <<= 1;
                while ($chr & 0x80) {
                    $i++;
                    $chr <<= 1;
                }
            }
        }
        return $count;
    }

   //求utf-8 子字符串。
       function  subStringUtf8($str, $start, $lenth) {
        $len = strlen($str);
        $r = array();
        $n = 0;
        $m = 0;
        for ($i = 0; $i < $len; $i++) {
            $x = substr($str, $i, 1);
            $a = base_convert(ord($x), 10, 2);
            $a = substr('00000000' . $a, -8);
            if ($n < $start) {
                if (substr($a, 0, 1) == 0) {

                } elseif (substr($a, 0, 3) == 110) {
                    $i += 1;
                } elseif (substr($a, 0, 4) == 1110) {
                    $i += 2;
                }
                $n++;
            } else {
                if (substr($a, 0, 1) == 0) {
                    $r[] = substr($str, $i, 1);
                } elseif (substr($a, 0, 3) == 110) {
                    $r[] = substr($str, $i, 2);
                    $i += 1;
                } elseif (substr($a, 0, 4) == 1110) {
                    $r[] = substr($str, $i, 3);
                    $i += 2;
                } else {
                    $r[] = '';
                }
                if (++$m >= $lenth) {
                    break;
                }
            }
        }
        return join("", $r);
    }
    

When subtracting with 4 decimal places, it is easy to lose precision, use mathematical functions.

$sum =   bcadd ( $a , $b , 4);   // a+b retains 4 decimal places. 
$diff   = bcsub ( $a , $b   , 4); // a- b to 4 decimal places. 
$mul = bcmul ( $a , $b , 4);     // a*b retains 4 decimal places. 
$div = bcdiv ( $a , $b , 4);     // a/b retains 4 decimal places and b cannot be 0.

5 Use transactions. (different framework codes are different, here is Yii as an example)

 $trans = Yii::app()->db->beginTransaction();   // Create a transaction. 
 $trans ->commit();    // transaction commit 
 $trans ->rollback();    // transaction rollback

 

6 Exception handling. (In the production environment, you do not want to expose errors to the outside world, and when the program is executed unexpectedly, the database is rolled back.) 

       try {
             $trans = Yii::app()->db-> beginTransaction();
             // ... intermediate execution code 
            $trans ->commit();   // transaction commit 
        } catch ( Exception  $e ) {
             log ( $e ->getMessage());   // Log record exception message 
            $trans ->rollback();   // Transaction rollback 
        }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325868636&siteId=291194637