Php global variable and regular expression error Solution

Source: Internet
Author: User
There are strict variable scopes in java, c, and c, while php does not have a clear (inaccurate) scope, for example, a variable defined outside a method-a method that cannot be referenced in the method can solve this problem by using a magic function or a global function such as the super global variable requestsessionpost, with the focus on global! A common PHP file, such

There are strict variable scopes in java, c, and c, while php does not have a clear (inaccurate) scope, for example, a variable defined outside a method-a method that cannot be referenced in the method can solve this problem by using a magic function or global such as the super global variable request session post, with the focus on global! A common PHP file, such

There are strict variable scopes in java, c, and c ++, while php does not have a clear (inaccurate) scope, for example, the variable-value defined outside a method cannot be referenced in the method.

To solve this problem, you can use the super global variable request session post or another magic function or global, with the focus on global! There is no problem in writing the following code in a common PHP file:

$a = 1;$b = 2;function Sum(){    global $a, $b; echo $a."  ",$b." ";    $b = $a + $b;}Sum();echo $b;//3return $b;



But it cannot be used in a php encapsulated class.

class MyClass {private  $str = "Hello, World";    function my_print() {        global $str;        print "str:".$str;    }}


Only in this way:

class MyClass {    function my_print() {        global $str;        print "str:".$str;    }}  $str = "Hello, World";$myclass = new MyClass();//$myclass->my_print();//Hello, World
I have been checking it online for a long time and found that global is invalid due to scope issues.

Some include operations may change the scope.


If you want to use global variables in the same class, try the following method:




 test1();$classtest->test2();print_r($operator_stack);//Array ( [0] => 1 [1] => 2 ) exit;
The following record uses php

preg_match
Regular Expression matching is always an error

The regular expression that has been debugged by java cannot be used in this method.

Solution: Add "//" at the beginning and end of your regular expression.

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.