PHP Combat Common code example

Source: Internet
Author: User

1 outputs an array in turn. Each input is 3 lines in a row.

In a small detail, it is said that $i < sizeof ($len) each cycle to calculate the length of the array, whether it is wasteful of performance. -----Answer: 1 You observe very fine. 2 Now the interpreter is smarter, and this problem is automatically optimized for processing.

 for ($i  $i<sizeof($len$i+ +)     {if$i$i Echo "<br>";    Echo $arr [$i];}

2 string does not allow special characters to appear.

        $chinese= "\x{4e00}-\x{9fa5}"; // Chinese match        $pattern= "/^[a-za-z0-9 ($chinese)]+$/u"; // plus English, digital matching        if (! Preg_match ($pattern,$nick _name)) {                exit("Do not allow special characters");        }

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

   //Utf-8 seeking utf-8 string length   functionStrlen_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; }   //The UTF-8 substring is evaluated.        functionSubStringUtf8 ($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); }    

4 decimal digit subtraction, easy to lose precision, using mathematical functions.

$sum=Bcadd($a,$b, 4);//A+b retains 4 decimal places. $diff=bcsub($a,$b, 4);//A -B retains 4 decimal places. $mul=Bcmul($a,$b, 4);//A*b retains 4 decimal places. $div=Bcdiv($a,$b, 4);//A/b reserved 4-bit decimal B cannot be 0.

5 using transactions. (Different framework code is not the same, here is the example of Yii)

$trans = Yii::app ()->db->begintransaction ();  // creates a transaction.  $trans->commit ();   // Transaction Commit $trans->rollback ();   // Transaction Rollback

6 exception handling. (In a production environment, you do not want to expose errors to the external, and when the program executes unexpectedly, the database rolls back processing.) )

       Try {            $trans= Yii::app ()->db->BeginTransaction (); //... Intermediate code of Execution            $trans->commit ();//Transaction Commit}Catch(Exception $e) {            Log($e->getmessage ());//Log record exception information            $trans->rollback ();//Transaction Rollback}

PHP Combat Common code example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.