"Master please near" PHP "global Reference passing" and "static statically variable" do not work

Source: Internet
Author: User
Tags parse error
Two separate PHP files: phpglobal.phpAnd phpstatic.php

phpglobal.php All contents:
 
  >>parse error:syntax error, unexpected ' & ', expecting t_variable or ' $ ' in D:\WebSite\MyHost\www.35dalu.com\pu blic_html\test\phpglobal.php on line 10*/function censorcount (& $string) {  Global & $censorCount;  $censorCount = (object) array (' banned ' = 0, ' mod ' = + 0, ' filter ' = 0);  if (Preg_match ("/Japan/I", $string))  {    $censorCount->banned++;  }  if (Preg_match ("/Korea/I", $string))  {    $censorCount->mod++;  }  if (Preg_match ("/Nina/I", $string))  {    $censorCount->filter++;    $string = Str_replace ("Nina", "Nima", $string);  }  return $string;} $arr = Array (' title ' = = ' is     the Korean more than a Japanese bull nose? ", ' content '   =" Korea than the Japanese bull nose? Whosaidso? Who! Who! ", ' author ' =    " Korea ratio "," $censorCount = "; foreach ($arr as $k = = $v) {  $arr [$k] = Censorcount ($v);} Print_r ($arr); Echo '
 
  ';p rint_r ($censorCount);





phpstatic.php All contents:
 
  >>array (    [title] = **nima** Korea than Japanese cattle nose?    [content] = Korea than Japanese cattle nose? Whosaidso? Who! Who!    [Author] = Korea Ratio)
 
  0*/function censorcount (& $string) {  static $censorCount;  static $CC = 0;  $censorCount = (object) array (' banned ' = 0, ' mod ' = + 0, ' filter ' = 0);  if (Preg_match ("/Japan/I", $string))  {    $cc + +;    $censorCount->banned++;  }  if (Preg_match ("/Korea/I", $string))  {    $cc + +;    $censorCount->mod++;  }  if (Preg_match ("/Nina/I", $string))  {    $cc + +;    $censorCount->filter++;    $string = Str_replace ("Nina", "**nima**", $string);  }  return $string;} $CC =0; $arr = Array (' title ' = = ' is     the Korean more than a Japanese bull nose? ", ' content '   =" Korea than the Japanese bull nose? Whosaidso? Who! Who! ", ' author ' =    " Korea ratio "," $censorCount = "; foreach ($arr as $k = = $v) {  $arr [$k] = Censorcount ($v);} Print_r ($arr); Echo '
 
  "echo"$cc";p rint_r ($censorCount);


What I want to do now is that each test will accumulate the results of the test.
In a string filter function but the inside of the test results can not be counted, that is, the program all the total number of mods after the count of banned number of filter number. But static statically variableAnd Global Introduction DeliveryIt's not working!


Reply to discussion (solution)

Don't look at the code carefully, but you can consider writing to a file, or using a database to store

$censorCount = (object) array (' banned ' = 0, ' mod ' = + 0, ' filter ' = 0);


Assignment,,, will erase all previous changes

This code, even if the implementation looks ugly enough, rewrite it in a class way.

Function Censorcount (& $string = ") {  static $censorCount;  static $CC = 0;  if (empty ($string)) return $censorCount;//Note here  if (empty ($censorCount)) $censorCount = (object) array (' banned ' = > 0, ' mod ' = 0, ' filter ' + 0);//Note here  if (Preg_match ("/Japan/I", $string))  {    $cc + +;    $censorCount->banned++;  }  if (Preg_match ("/Korea/I", $string))  {    $cc + +;    $censorCount->mod++;  }  if (Preg_match ("/Nina/I", $string))  {    $cc + +;    $censorCount->filter++;    $string = Str_replace ("Nina", "**nima**", $string);  }  return $string;} $CC =0; $arr = Array (' title ' = = ' is     the Korean more than a Japanese bull nose? ", ' content '   =" Korea than the Japanese bull nose? Whosaidso? Who! Who! ", ' author ' =    " Korea ratio "," $censorCount = "; foreach ($arr as $k = = $v) {  $arr [$k] = Censorcount ($v);} Print_r ($arr); Echo '
 
  "echo"$cc";p Rint_r (Censorcount ());//Note here
Another version is similar to this

PHP Code
Function Censorcount (& $string = ")
{
Static $censorCount;
static $CC = 0;
if (empty ($string)) return $censorCount;//Note here
if (empty ($censorCount)) $censorCount = (object) array (' Bann ...

Thanks, man.
But the value of that $CC variable doesn't change.
In addition I have a place of doubt.
(1) If static is used inside a function, does that variable need to be declared in advance?
(2) I found sometimes $obj = (object) array (); will be error, must write a non-empty array () will not. But sometimes I find that I write directly $obj = (object) array (); No error, but sometimes it will be an error.
(3) Use Global & $var within functions for reference passing within a function; Is there a space between the & and the $var ? or the & and the $var are connected together.
(4) The problem of passing the function reference. Just knowing that a variable reference pass is a change within a function, the outside of the function also changes. What is the function of reference passing? It has been hard to understand ...

$obj = (object) array (); Sometimes you get an error? I've seen it for a while, and of course I don't use it much.
& $var No spaces required
Function reference return more in the object bar, such as referencing an object variable many, you want to use up after the human flesh cleanup, you can use & $test=null

Dude, that's a problem too! Look closely at the handbook and see the examples in one sentence.

Your code is too long.


(1) If static is used inside a function, does that variable need to be declared in advance?

Static is a declaration, and it can be assigned a value. only works once.

(2) I found sometimes $obj = (object) array (); will be error, must write a non-empty array () will not. But sometimes I find that I write directly $obj = (object) array (); No error, but sometimes it will be an error.

If the $obj is already quoted, or can be an error. Normal situation, no error. This is self-testing to see it! Look at the example of the handbook.

(3) The use of global & $var within functions for reference passing within a function; Immediate error. Is there a space between the & and the $var? or the & and the $var are connected together.

Global itself is a reference and does not require &.

(4) The problem of passing the function reference. Just knowing that a variable reference pass is a change within a function, the outside of the function also changes. What is the function of reference passing? It has been hard to understand ...

If the variable is not declared, it is created automatically. The function is very simple ah, under normal circumstances, the variable within the function is the local change. A reference to a variable that can manipulate the global directly.

The previous feature is that object references make sense. But php5, all objects are referenced in their own way. No, it's all right. Only variable references make sense.

This makes sense when designing patterns, and you can easily pass objects to any other object. Reference to refer to go!

First knot paste. The reference pass for function is not very understandable. ~

  • 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.