Learning notes for php global variables

Source: Internet
Author: User
Tags php basics rtrim traits

Today, we will refer to php official introduction to php global variables to understand some usage of php global variables and some thoughts on it. Now we hope to help you.


I. Key points:

1. What global variables are expired and how to deal with them?
For example: unset ($ GLOBALS, $ _ ENV, $ HTTP_GET_VARS, $ HTTP_POST_VARS, $ HTTP_COOKIE_VARS, $ HTTP_SERVER_VARS, $ HTTP_ENV_VARS );
2. Which global variables are invalid under the command line?
3. Why should I use htmlspecialchars?
4. What are the connection and differences between $ _ REQUEST and $ _ GET, $ _ POST, and $ _ COOKIE?
5. Why does the $ _ FILES variable not exist or is it empty when a file is uploaded?
6. What is the scope of global variables?
7. How can I obtain the user's IP address and browser's user_agent information?
8. I don't use a browser. I'm socket programming? How to keep the session?
9. How to obtain headers information?


Nine global predefined arrays in php

Hyper-global variables are built-in variables that are always available in all scopes.
$ GLOBALS-reference all available variables in the global scope
$ _ SERVER-SERVER and execution environment information
$ _ GET-http get variable
$ _ POST-http post variable
$ _ FILES-variables for uploading HTTP FILES
$ _ REQUEST-HTTP Request variable
$ _ SESSION-Session variable
$ _ ENV-Environment Variable
$ _ COOKIE-HTTP Cookies
$ Php_errormsg-previous error message
$ HTTP_RAW_POST_DATA-native POST data
$ Http_response_header-HTTP Response Header
$ Argc-number of parameters passed to the script
$ Argv-parameter array passed to the script

Ii. Main Problems
==========================================

PHP basics summary:
1>. var_dump (0 = "ads"); what is the result of this statement? Why?
A: the result of this statement is true;
The string on the right of this expression is forcibly converted to int DATA 0 by default, which is the same as the data on the left. Therefore, true is output. If it is "=", false is output, this is because they have different types and different values, and will not be forcibly converted here.

Comment: This is really hard to say, test it! Var_dump () prints the expression result! This is actually an expression operation.

2> can an exception be cloned?
A: No.

3> what are the features of Traits in PHP?
A: Traits (horizontal reuse/Multi-inheritance) is a group of class-like methods, but it cannot be instantiated. It allows developers to easily reuse methods in different classes;
The best application of Traits is that multiple classes can share the same functions;
Static variables can be referenced by the trait method, but cannot be defined by trait. However, trait can define static methods for the classes used;
If trait defines an attribute, the class cannot define the attribute with the same name; otherwise, an error occurs;

Conclusion: The answers to the questions are good.

============================================

1. What super global variables are there? A: Super global variables: $ GLOBALS, $ _ SERVER, $ _ GET, $ _ POST, $ _ FILES, $ _ COOKIE, $ _ SESSION, $ _ REQUEST, $ _ ENV

2. the browser disabled the cookie. Can $ _ SESSION be used? A: it cannot be used.
Comment: an error is returned. It can be used! Apache has an automatic function to put session_id in the get parameter.

3. When is $ php_errormsg available? A: No. This variable is only available in the scope where the error occurs, and the track_errors configuration item is enabled (disabled by default ).

Conclusion: The answer is positive, and it is better to be full.

 

==========================================================

There is a user-friendly interview question
Write 30 php string functions.
Example:
Trim () removes spaces from the string
Chop () removes the specified character from the right to the left. The parameter rtrim (string, find );
Rtrim () removes the specified character from the right to the left. The parameter rtrim (string, find );
Chr () returns the asc2 code of the character.
Var_dump () prints a value, which can be any form and return the attribute
Print () print an array or string
Print_r () Prints strings and numbers, while arrays are displayed in the form of an enclosed key and a list of values.
Chunk_split () splits the string into a series of smaller parts
Implode () combines array elements into a string
Join () combines array elements into a string
Explode () Splits strings into Arrays
Md5 () returns an md5 value, which is irreversible.
Strlen () gets the length of a string
Str_replace replaces some characters in the string
Mb_substr mb extended truncation string function, format: mb_substr (string, start_Num, end_Num, 'utf-8 ')
Str_split Splits a string into an Array Based on the Character Spacing
Strpos searches for and returns the position of the first matching item
Special characters in the strtr conversion string
Substr truncates a string.
Substr_count calculates the number of occurrences of a character segment in a string.
Substr_replace replaces some characters in the string
The wordwrap function returns a string with a specified length.
Addcslashes () Add a backslash before the specified character
Strtolower () converts string to lowercase
Strtoupper () converts string to uppercase
Strrev () reverse string
Strripos () is used to locate the last position of a string in another string (Case Insensitive)
Strrpos () searches for the position (case sensitive) of the last occurrence of a string in another string)
Returns the number of characters contained in the string.
Ucwords () converts the first character of each word in a string to uppercase
Str_repeat () indicates the number of times the string is repeated.
...

========================================================

1. Must the namespace be the first statement of the program script?
Answer: yes

2. How do I cancel a reference?
Answer: unset ();

3. if at least one method in a class is declared as abstract, must this class be declared as an abstract class? When inheriting an abstract class, must the subclass define all abstract methods in the parent class?
Answer: If an abstract method exists in a class, the class must be defined as an abstract class. When inheriting an abstract class, the subclass must define all abstract methods in the parent class.

==========================================================

1. How to obtain the absolute path of the file
Use the realpath () function to return the absolute path name. If the file fails, false is returned. For example, the file does not exist.
Echo realpath ("test.txt ");
Output:
C: wwwtestwebtest.txt

2. How the session works
Session is saved on the server, but a session ID is saved as a cookie on the client. If the cookie is disabled, you need to use the URL rewriting mechanism of the get method or the form of submitting hidden forms using the POST method.

Comments: This is the principle, but generally some web servers will automatically complete this function, and do not need to be added to the program.

3. Is the public static declared function a static method?
The public static declared function is a static method, which can be used directly for external use of the class. The class name: The method call of the function name does not need to use the NEW declaration method.
Note: The function cannot contain dynamic content such as $ this->.
In general, the content that requires frequent execution should be declared using STATIC statements.

Conclusion: the working principles of sessions are well organized and thoroughly analyzed. We hope to continue our efforts in the future.

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.