Ask questions about the php variable scope ~~~

Source: Internet
Author: User
{Code...} Excuse me: I am listening to a php video from a teacher for 11 years, but the teacher's Code does not use static $ string. But no error is reported, but if I do not add static, the error will be reported, and the teacher will not use @. How can I define the variable so that it can be used in the function.
   function sub($str,$len){        for($i=0;$i<$len;$i++){            static $string;             if(ord(substr($str,$i,1))>0xa0){                  $string.=substr($str,$i,2);                   $i++;             }else                 $string.=substr($str,$i,1);             }        }        return $string;    }

Excuse me, I am listening to my 11-year php video from a teacher, but the teacher's code is not used.
Static $ string. But no error is reported, but if I do not add static, the error will be reported, and the teacher will not use @.
How can I define the variable so that it can be used in the function.

Reply content:
   function sub($str,$len){        for($i=0;$i<$len;$i++){            static $string;             if(ord(substr($str,$i,1))>0xa0){                  $string.=substr($str,$i,2);                   $i++;             }else                 $string.=substr($str,$i,1);             }        }        return $string;    }

Excuse me, I am listening to my 11-year php video from a teacher, but the teacher's code is not used.
Static $ string. But no error is reported, but if I do not add static, the error will be reported, and the teacher will not use @.
How can I define the variable so that it can be used in the function.

Use $ string as a parameter and do not need to add static. In this way, string is a global variable outside, and a local variable after passing in the function;
Then return the $ string, which makes the program clearer and easier to understand.

function sub($str,$len,$string){        for($i=0;$i<$len;$i++){             $string;             if(ord(substr($str,$i,1))>0xa0){                  $string.=substr($str,$i,2);                   $i++;             }else                 $string.=substr($str,$i,1);             }        }        return $string;    }

$ String =. I don't understand why static is used

The variables used in the function are global variables if they are not passed in.

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.