I have seen the @ operator before the function, and I have also considered its function. but I just didn't remember it. now I will review it later and show it to my friends who have the same questions!
I have seen the @ operator before the function, and I have also considered its function. but I just didn't remember it. now I will review it later and show it to my friends who have the same questions!
@ Operator is only valid for expressions. A simple rule for beginners is: if you can get a value from somewhere, you can add the @ operator before it. For example, you can place it before variables, functions, include () calls, constants, and so on. It cannot be placed before the definition of a function or class, nor can it be used for condition structures such as if and foreach.
The current "@" error control operator prefix even invalidates error reports that cause severe errors caused by script termination. This means that if "@" is used to suppress the error message before a function call that does not exist or has a type error, the script will not show any evidence of the cause and die there.
Simply put, it is to suppress the output errors of this statement. if some errors cannot be seen by users, add @
For example: @ mysql_connect (...);
In PHP, the & symbol refers to the reference of the variable rather than the copy. the reference means that different names are used to access the content of the same variable. this is not like the pointers of C. They are symbol table aliases. note that in PHP, the variable name and variable content are different, so the same content can have different names. the closest analogy is the Unix file name and the file itself-the variable name is a directory entry, while the variable content is the file itself. references can be seen as a tight connection in Unix file systems.
PHP references allow you to use two variables to point to the same content. This means that when you do this:
This means that $ a and $ B point to the same variable.
Note: $ a and $ B are exactly the same here. this is not because $ a points to $ B or vice versa, but $ a and $ B point to the same place.
The same syntax can be used in functions, which return references, and in the new operator (PHP 4.0.4 and later versions ):
Note: No & operator is used to generate a copy of the object. if you use $ this in the class, it will act on the current instance of the class. the instance (such as an object) will be copied without a value & and $ this will apply to this copy, which is not always the desired result. because of performance and memory consumption problems, you usually only want to work on an instance.
Although you can use the @ operator to disable any error information in the constructor, for example, @ new, it does not work when the & new statement is used. this is a limitation of the Zend Engine and may cause a parsing error.
The second thing to do by referencing is to pass variables using references. this is achieved by creating a local variable in the function and referencing the same content within the call range. for example:
Change $ a to 6. this is because the variable $ var points to the same content as $ a in the foo function. For more details, see references.
The third thing to reference is to return a reference.
Reference is not
As mentioned above, the reference is not a pointer. This means that the following structure will not produce your expected results:
This binds the $ var variable in the foo function to $ bar when calling the function, but is then re-bound to $ GLOBALS ["baz. it is impossible to bind $ bar to another variable within the function call range through the reference mechanism, because there is no variable $ bar in function foo (it is represented as $ var, but $ var only contains the variable content and does not call the name-to-value binding in the symbol table ).
Reference transfer
You can pass a variable to the function through reference, so that the function can modify the value of its parameter. syntax:
Note that there is no reference symbol in the function call-only the function definition is available. just the function definition is enough to make the parameter pass through the reference correctly.
The following content can be passed through reference:
Variable, such as foo ($)
New statement, such as foo (new foobar ())
Reference returned from the function, for example:
No other expressions can be passed by reference and the result is undefined. for example, the example passed by reference below is invalid:
These conditions are available in PHP 4.0.4 and later versions.
Reference return
When you want to use the function to find the variable to which the reference should be bound, use this syntax when returning the reference:
x = 2; ?>
In this example, the attributes of the objects returned by the find_var function will be set (translator: $ foo-> x = 2; statement), instead of copying the objects, just like not using the reference syntax.
Note: Unlike parameter transfer, the & symbol-must be used in both places to indicate that a reference is returned instead of a normal copy. it is also pointed out that $ foo is bound as a reference, instead of assigning values.
Cancel reference
When you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. for example:
Not unset $ B, just $.
It may be helpful to understand the Unix unlink call.
Reference and positioning
*** PHP syntax structure is implemented through the reference mechanism. Therefore, all the above mentioned references and bindings apply to these structures. some structures, such as reference transfer and return, have been mentioned above. other referenced structures include:
Global Reference
When a global $ var variable is declared, a reference to the global variable is actually created. that is to say, it is the same as doing so:
This means that, for example, unset $ var does not unset global variables.
$ This
In the method of an object, $ this is always a reference to the object that calls it.