[Master, please visit] PHP [global reference transfer] and [static variable] do not work

Source: Internet
Author: User
[Master, please visit] PHP [global reference transfer] and [static variable] do not work two separate PHP files: PhpGlobal. phpAnd PhpStatic. php

All PhpGlobal. php content:
 > Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in D: \ WebSite \ MyHost \ www.35dalu.com \ public_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_m Atch ("/nima/I", $ string) {$ censorCount-> filter ++; $ string = str_replace ("nima", "nima", $ string );} return $ string ;}$ arr = array ('title' => "is Nima cool better than Japan? ", 'Content' =>" is Korea better than Japan? WHOSAIDSO? WHO! WHO! ", 'Author' =>" Korea ratio ",); $ censorCount =''; foreach ($ arr as $ k => $ v) {$ arr [$ k] = censorCount ($ v);} print_r ($ arr); echo'
 '; Print_r ($ censorCount );





All PhpStatic. php content:
 > Array ([title] => ** nima ** is Korea better than Japan? [Content] => is Korea better than Japan? 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 ("/Nima/I", $ string) {$ cc ++; $ censorCount-> filter ++; $ string = str_replace ("Nima ", "** nima **", $ string);} return $ string; } $ Cc = 0; $ arr = array ('title' => "is Nima better than Japan? ", 'Content' =>" is Korea better than Japan? WHOSAIDSO? WHO! WHO! ", 'Author' =>" Korea ratio ",); $ censorCount =''; foreach ($ arr as $ k => $ v) {$ arr [$ k] = censorCount ($ v);} print_r ($ arr); echo'
 '; Echo"$ Cc"; Print_r ($ censorCount );


What I want now is: each detection will accumulate the detection results.
When a function is used to filter strings, the detection results in the function cannot be counted, that is, after the program detects all the results, the total number of mod and banned counts the number of filters. However Static variablesAnd Global import transferDoes not work!


Reply to discussion (solution)

You don't have to read the code carefully, but you can consider writing it into a file or using a database to store it.

$ CensorCount = (object) array ('banned' => 0, 'mod' => 0, 'filter' => 0 );


Assign a value, and all previous changes will be erased.

Your code looks ugly even if it is implemented. use the class method to rewrite it.

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 that if (preg_match ("/Japan/I", $ string) {$ cc ++; $ censorCount-> banned ++;} if (preg_match ("/Korea/I", $ string) {$ cc ++; $ censorCount-> mod ++ ;} if (preg_match ("/Nima/I", $ string) {$ cc ++; $ censorCount -> Filter ++; $ string = str_replace ("nima", "** nima **", $ string);} return $ string ;}$ cc = 0; $ arr = array ('title' => "is Nima better than Japan? ", 'Content' =>" is Korea better than Japan? WHOSAIDSO? WHO! WHO! ", 'Author' =>" Korea ratio ",); $ censorCount =''; foreach ($ arr as $ k => $ v) {$ arr [$ k] = censorCount ($ v);} print_r ($ arr); echo'
 '; Echo"$ Cc"; Print_r (censorCount (); // pay attention to this
Another version is similar to this one.

PHP code
Function censorCount (& $ string = '')
{
Static $ censorCount;
Static $ cc = 0;
If (empty ($ string) return $ censorCount; // note the following:
If (empty ($ censorCount) $ censorCount = (object) array ('bann ......

Thank you.
But the value of the $ cc variable has not changed.
In addition, I have a question.
(1) If static is used in the function, do you need to declare the variable in advance?
(2) Sometimes I find$ Obj = (object) array ();An error is reported. a non-empty array () is required. But sometimes I find that$ Obj = (object) array ();No error is reported, but sometimes an error is reported.
(3) use of references passed in a functionGlobal & $ var;An error is reported immediately. That&And$ VarAre spaces required between them? Or connect with $ var.
(4) function reference transfer problems. It is only known that the variable reference transfer is changed within the function, so it also changes outside the function. So what is the function reference transfer function? It's hard to understand ............

$ Obj = (object) array (); sometimes an error is reported? I have met Mao for the moment. Of course I don't use much.
& $ Var no space required
Function Reference returns more common to objects. for example, if you reference a large number of object variables, you can use &. and then $ test = null if you want to clean up future generations.

This is also a problem, dude! Take a closer look at the manual and give an example.

Your code is too long.


(1) If static is used in the function, do you need to declare the variable in advance?

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

(2) I found that sometimes $ obj = (object) array (); will report an error, it must be written into a non-empty array. However, sometimes I find that writing $ obj = (object) array (); will not report an error, but sometimes it will report an error.

If $ obj is already referenced, an error may be returned. Normally, no error is reported. Let's test this! See the example in the manual.

(3) use global & $ var; in the function to report an error immediately when the reference is passed in the function. Do I need a space between that & and $ var? Or connect with $ var.

Global itself is a reference and does not need &.

(4) function reference transfer problems. It is only known that the variable reference transfer is changed within the function, so it also changes outside the function. So what is the function reference transfer function? It's hard to understand ............

If the variable is not declared, it is automatically created. The function is simple. under normal circumstances, variables are changed in the function. Reference variables that can be operated on globally.

In the past, object references were meaningful. However, in php5, all objects are passed through the reference method. You can use it. Only variable references make sense.

This makes sense in the design mode, and you can easily pass the object to any other object. Reference!

Paste it first. I am not very familiar with function reference transfer .~

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.