Php comprehensive exercises

Source: Internet
Author: User
Tags type null
1. how to define variables? How do I check whether variables are defined? How to delete a variable? Determines whether a variable is null? Isset () unset () empty () 2. what is a variable? A variable name can be dynamically set and used. $ A = & amp; #39; hello & amp; #39;, $ a... "/> <scripttype =" text/ja

1. how to define variables? How do I check whether variables are defined? How to delete a variable? Determines whether a variable is null?
Isset ()
Unset ()
Empty ()

2. what is a variable?
A variable name can be dynamically set and used.
$ A = 'hello', $ a = 'world', $ {$ a} = hello world

3. how can I assign values to variables?
1) direct assignment
2) assign values between variables
3) reference assignment

4. what is the difference between reference and copy?
The copy operation copies the original variable content. the copied variable and the original variable use their own memory, which does not affect each other.
Reference is equivalent to the alias of a variable. In fact, different names are used to access the same variable content. When you change the value of one of the variables, the other also changes.


5. What are the basic data types of variable quantities in php?
Php supports eight types of raw data.
Including:
Four scalar types (boolean, integer interger, float/double, string)
Two composite types (array, object)
Two special types (resource, NULL)


6. Which of the following values are considered false when other types are converted to the boolean type?
Boolean value false, integer value 0, floating point value 0.0, blank string, string '0', empty array, special data type NULL, no set variable.

When does the empty () function return true?
Boolean value false,
Integer value 0,
Floating point value 0.0,
Blank string,
String '0 ',
Array () empty array,
Special data type NULL,
Objects without any attributes,
No variable assigned.


7. if a variable $ a is defined, but no initial value is assigned
So $ a = 0?
$ A = false?
$ A =?
$ A = NULL?
$ A === NULL? A: echo => nothing, var_dump => NULL
Empty ($ B) = true? ------------ Echo => 1, var_dump => bool (true)
What is the output value of $ a ++? ------- Echo => nothing, var_dump => NULL
What is output + + $? --------- Echo => 1, var_dump => int (1)


8. how can I convert a string to an integer? How to implement it?
Forced type conversion: (integer) string variable name;
Direct conversion: settype (string variable, integer );
Intval (string variable );

9. what is the biggest difference between scalar data and arrays?
A scalar can only store one data, while an array can store multiple data.


10. how do I define constants? How can I check whether a constant is defined? Which data types can be the constant value?
Define () // define a constant, defined () // check whether the constant is defined
Constant values can only be scalar data.


11. constants are classified into built-in constants and custom constants. What are the most common built-in constants?
_ FILE _, _ LINE _, PHP_ OS, PHP_VERSION

12. if two identical constants are defined, which of the former and latter functions?
The former works because a constant cannot be redefined or undefined once defined.


13. What are the differences between constants and variables?
1) there is no $ symbol before the constant;
2) constants can only be defined through define (), but not through the value assignment statement;
3) constants can be defined and accessed anywhere, and variables are classified as global and local;
4) once defined, constants cannot be redefined or undefined, while variables can be redefined by assigning values;
5) the constant value can only be a scalar data, and the variable database type has eight original data types.


14. Which of the following predefined global array variables are commonly used in PHP?
There are nine predefined built-in array variables:
$ _ POST, $ _ GET, $ _ REQUEST, $ _ SESSION, $ _ COOKIE, $ _ FILES, $ _ SERVER, $ _ ENV, $ GLOBALS

15. in actual development, where are constants most commonly used?
1) database connection information is defined as constants, such as the username, password, database name, and host name of the database server;
2) define some site paths as constants, such as the absolute web path, the installation path of smarty, and the folder path of model, view, or controller;
3) public information of the website, such as the website name and keyword.

16. What are the advantages of functions?
Improve program maintainability
Improve Software reliability
Improve program reusability
Improve program development efficiency


17. how to define a function? Is the function name case sensitive?
1) use the function keyword;
2) function naming rules are the same as variables. they must start with a letter or underscore rather than a number;
3) the function name is case insensitive;
4) the function name cannot be a declared or self-built function name.


18. what is the visibility of a variable or the scope of a variable?
Is the scope of the variable in the program. According to the visibility of variables, variables are divided into local variables and global variables.


19. What are local variables and global variables? Can global variables be directly called in the function?
A local variable is a variable defined inside a function, and its scope is the function. If a variable with the same name as a local variable exists,
The program considers the two variables to be completely different. When you exit the function, the local variables are cleared at the same time.

Global variables are defined outside all functions, and their scope is the entire php file, but they cannot be used within the user-defined functions.
If you must use global variables within a user-defined function, you must use the global keyword declaration.
That is to say, if golbal is added before the variable in the function, the global variable can be accessed within the function,
Not only can this global variable be used for calculation, but the global variable can be re-assigned.
You can also use $ GLOBALS ['var'] to call global variables.


20. how to use the global keyword? How to use a predefined global variable array $ GLOBALS?


21. What are static variables?
If a variable defined in a function is declared using the keyword static, the variable is a static variable.
Generally, after a function is called, the stored data is cleared and the occupied memory space is released. When static variables are used,
This variable will be initialized when the function is called for the first time, and the variable will not be cleared after initialization. when the function is called again, this static variable
Instead of being initialized, you can save the value after the last function is executed. Static variables are shared among all calls to the function.


22. how does a function pass parameters in php? What is the difference between the two?
Pass by value and by address (or by reference)
(1) pass by value: the variables to be passed are stored in different spaces as they are passed to the function. So the function body
The modification of the variable value does not affect the original variable value.
(2) pass by address: Use the & symbol to indicate that this parameter is passed by address. It does not pass the specified value or target variable in the main program to the function,
Instead, the memory block address of the value or variable is imported into the function. Therefore, the variable in the function body and the variable in the main program are stored in the memory.
Is the same. Changes made to the function body directly affect the value of this variable outside the function body.


23. what is a recursive function? How do I perform recursive calls?
Recursive functions actually call their own functions, but must meet the following two conditions:
1) each call must be closer to the final result;
2) there must be a definite recursive termination condition that will not cause an endless loop.
Example:
In practice, it is often used to traverse folders.
If you want to obtain all the files in the windows directory, traverse the windows directory first. if you find that there are still folders, you will call the directory itself, continue searching, and so on,
This means that all files are traversed.


24. check whether a function exists?
Function_exists (string $ function_name) if it exists, true is returned. if it does not exist, false is returned.

25. what is the difference between func () and @ func?
If the second function fails to be called, no error is reported. if the first function fails to be called, no error is returned.

26. What are the usage and differences between the include () and require () functions? What about include_once () and require_once?
The include and require error levels are different.
Before loading include_once () and require_once (), you must determine whether the data has been imported.

27. what is the difference between front ++ and back ++?
In front ++, you first increase the variable by 1 and then assign the value to the original variable;
+ + Is to first return the current value of the variable, and then increase the current value of the variable by 1.


28. what is the difference between the string operator "." and the arithmetic operator "+?
It is considered a hyphen when "a" and "B" are used. If the two are +, php considers them an operation.
1) if the strings on both sides of the plus sign are composed of digits, the strings are automatically converted into integers;
2) if both sides of the plus sign are pure letters, 0 is output;
3) If the strings on both sides of the plus sign start with a number, the numbers starting with the string are truncated and then computed.


29. what is a three-object (or three-element) operator?
Select one of the other two expressions based on the results of one expression.
For example: ($ a = true )? 'Good': 'bad ';


30. What are the control flow statements?
1: three Program Structures: sequential structure, branch structure, and cyclic structure
2: Branch: if/esle/esleif/switch/case/default
3: switch:
Constants in the case clause can be integer, string-type constants, or constant expressions, but cannot be variables.
In the same switch clause, the value of case cannot be the same; otherwise, the value of case can only be obtained for the first time.
4: loop for while do... while
Do... while must be followed by a semicolon.
Difference Between while and do... while
5: What is the difference between break and continue.
Break can terminate the loop.
Continue is not as powerful as break. it can only terminate this cycle and enter the next cycle.


31. what is the concept of array? What are the two types of arrays based on indexes? Which of the following methods can assign values to an array?
An array is a variable (compound variable) that can store a group or a series of values)
Index array (index value is a number, starting with 0) and associated array (using string as index value)

Which of the following methods can assign values to an array?
There are two methods to declare arrays.
1. declare an array through the array () function;
You can use key => value to define the index and value respectively, or you can not define the index subscript of the array, only the element values of the array are given.
2. assign values directly to array elements without calling the array () function. For example:
$ Arr [0] = 1;
$ Arr [1] = 2;
Note:
If the subscript of an array is equivalent to the string value of an integer (but cannot start with 0), it is treated as an integer.
For example, $ array [3] and $ array ['3'] reference the same element, $ array ['03'] references another element.


32. how to traverse arrays?
① For loop
② Foreach loop, which is the most common traversal method. Usage: foreach ($ arr as $ key => $ value ){}
③ List each and while are used together for loop


33. how does the pointer point to the foreach array? How does a pointer point to a list ()/each ()/while () loop array?
When foreach starts execution, the pointer inside the array automatically points to the first unit. Because foreach operates on copying the specified array, rather than the array itself.
After each () is an array, the array pointer will stay in the next unit of the array or the last unit when it comes to the end of the array. If you want to use each () to traverse the array again, you must use reset ().
Reset () returns the internal pointer of the array to the first unit and the value of the first array unit.


34. how to calculate the array length (or calculate the number of all elements in the array )? How to obtain the length of a string?
Count () -- calculate the number of elements in the array.
You can use count (array name) or count (array name, 1). if there is a second parameter and the number is 1, the number of array elements is calculated recursively.
If the second parameter is 0, it is equivalent to the count () function with only one parameter.
Alias of sizeof () -- count ()
String: strlen (), mb_strlen ();


35. What are common functions related to the array?
1) count -- (sizeof alias)-calculate the number of units in the array or the number of attributes in the object
For example, int count (mixed $ var [, int $ mode]) $ var is usually an array type, and any other type has only one unit. The default value of $ mode is 0. 1. enables recursive array counting.
2) in_array (mixed $ needle, array $ haystack [, bool $ strict])-check whether a value exists in the array.
If the needle is a string, the comparison is case sensitive.
If the value of the third strict parameter is TRUE, the in_array () function checks whether the needle type is the same as that in haystack.
3) array_merge (array $ array1 [, array $ array2 [, array $...]) combine the elements of one or more arrays. the values of an array are appended to the values of the previous array. Returns an array of results.
Note: If the input array contains the same string key name, the value after the key name overwrites the previous one. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.
If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously.

4) conversion between arrays and strings
(1) explode (string $ separator, string $ string [, int $ limit]) uses a separator character to separate a string.
(2) implode (string $ glue, array $ arr) concatenates each unit in the array into a string using a connector.
Join is the alias of implode

5) sort (array & $ array [, int $ sort_flags])-sorts arrays by values. when this function ends, group units are rescheduled from the lowest to the highest.


36. what is the difference between the array merging function array_merge () and the array addition operation $ arr + $ arr2?
Array_merge ()-> use array_merge (). if it is an associated array combination, if the key name of the array is the same, the subsequent values will overwrite the former; if it is a digital index array combination, instead of overwriting
The latter is appended to the latter.
"+"-> Using array addition operations, unlike array_merge (), addition operations discard values with the same key name, whether associated arrays or numeric index arrays,
That is, only the elements with the same key name will be retained for the first time.


37. what is the difference between single quotation marks and double quotation marks when a string is defined?


38. What are the differences between echo (), print (), and print_r?
(1) echo is the syntax, Output one or more strings, and no return value;
(2) print is a function that cannot Output arrays and objects. Output a string and print have returned values;
(3) print_r is a function that can output arrays. Print_r is an interesting function that can output stirng, int, float,
Array, object, etc. The structure is used to output the array. true is returned when the print_r output is successful. print_r can use print_r ($ str, true, make print_r output and return the value after print_r processing. In addition, echo and print are mostly used because they are more efficient than print.


39. which string processing functions are available according to the function category? What are the functions of these functions?
A. string output function
(1) echo $ a, $ B, $ c...; is a language structure, not a real function.
(2) print ($ a) the output string of this function. If the call succeeds, 1 is returned. if the call fails, 0 is returned.
(3) print_r ($)
(4) var_dump ($ a); outputs the type, length, and value.
B. function for removing spaces at the beginning and end of a string: trim ltrim rtrim (alias: chop) uses the second parameter and can also remove specified characters.
C. escape string function: addslashes ()
D. function for obtaining the string length: strlen ()
E. function for intercepting string length: substr ()
F. search string functions: strstr (), strpos ()
G. string replacement function: str_replace ()


40. Are you sure you want to answer the following questions?
1). $ arr = array ('James ', 'Tom', 'symfony '); use', 'to split the $ arr array values and combine them into strings for output?
Echo implode (',', $ arr );

2). $ str = 'Jack, james, tom, symfony '; split $ str with', 'and put the split value in the $ arr array?
$ Arr = explode (',', $ str );

3 ). $ arr = array (, 'D', 'ABC'); sort $ arr in ascending order and keep its key value unchanged?
Arsort ($ arr); print_r ($ arr );

4). $ mail = "gaofei@163.com"; please take out the domain (163.com) of this mailbox and print it, to see how many methods can be written?
Echo strstr ($ mail, '123 ');
Echo substr ($ mail, 7 );
$ Arr = explode ("@", $ mail); echo $ arr [1];

5). if there is a string, the string is "123,234,345 ,". How can I cut out the last comma of this string?

6) What are the functions used to obtain random numbers? Which of the mt_rand () and rand () processes fast?


41. page characters are garbled. how can this problem be solved?
1. check whether the character set is set for the current file. Check whether charset is written in the meta tag. if it is a php page, you can check whether it is
Charset is specified in the header () function;
For example:

Header ("content-type: text/html; charset = utf-8 ");

2. if the character set (charset) is set, check whether the encoding format of the current file is consistent with the character set on the page,
The two must be consistent;

3. if it involves extracting data from the database, determine whether the character set in the database query is consistent with the character set on the current page. The two must be consistent,
For example, mysql_query ("set names utf8 ").


42. what is a regular expression? Which regular-related functions are commonly used in php? Please write an email regular expression for the Chinese mobile phone number and landline number?
Regular expressions are a syntax rule used to describe the character arrangement mode. A regular expression is also called a pattern expression.
Regular expressions in website development are most often used for client verification before form submission information.
For example, verify that the user name is correct, the password is correct, and the email, mobile phone number, and other information is valid.
In php, regular expressions are mainly used for string segmentation, matching, search, and replacement.

The preg series functions can be processed. There are the following details:

String preg_quote (string str [, string delimiter])
Escape regular expression characters special characters of the regular expression include:. \ + *? [^] $ () {}=! <> | :.
Preg_replace -- Search and replace regular expressions
Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Preg_replace_callback -- use the callback function to search and replace regular expressions.
Mixed preg_replace_callback (mixed pattern, callback, mixed subject [, int limit])
Preg_split -- use a regular expression to split a string
Array preg_split (string pattern, string subject [, int limit [, int flags])

 

43. which function is used to filter out all html tags of a string?


44. What are the differences between the preg_replace () and str_ireplace () functions? How do preg_split () and split () functions be used?


45. What are the main functions used to obtain the current timestamp? Print the current time in PHP. the format is 22:21:21?
Print the time format of the previous day in PHP: 22:21:21? How does one change 10:30:25 to a unix timestamp?

Echo date ("Y-m-d H: I: s", strtotime ('-1, days '));

Date ('Y-m-d H: I: S', time ());

$ Unix_time = strtotime ("10:30:25"); // The unix timestamp.
Echo date ("Y-m-d H: I: s", $ unix_time); // format it in normal time format


46. which function should be used to encode Chinese characters in case of garbled characters when using get to pass values in URLs?
When a user submits data in a website form, to prevent script attacks (for example, when the user inputs script alert (111) and script, what should I do?
Use urlencode () to encode Chinese characters and urldecode () to decode them.
You can use htmlspecialchars ($ _ POST ['title']) to filter input parameters in a form to avoid script attacks.


47. What are the steps for connecting to the database? What data type is returned for each step? In particular, what data types does mysql_query () return?

48. what is the difference between mysql_fetch_row () and mysql_fetch_assoc () and mysql_fetch_array?
The first is to return a row in the result set as the index array, the second is to return the associated array, and the third can return both the index array and the associated array, it depends on its second parameter MYSQL_BOTH MYSQL_NUM MYSQL_ASSOC. the default value is MYSQL_BOTH.
$ SQL = "select * from table1 ";
$ Result = mysql_query ($ SQL );
Mysql_fetch_array ($ result, MYSQL_NUM );

49. have you learned how to return the resource function?
A: mysql_connect ();
Mysql_query (); the resource is returned only when the select statement is executed successfully. if the select statement fails, FALSE is returned.
Fopen ();

50. what functions are used to open or close a file? What is a file read/write function? Which function is used to delete files?
Which function does a file have? Which function is used to create a directory?


51. What are the details of file upload? How to save the file to a specified directory? How can I avoid the problem of duplicate names of uploaded files?
1. enable file upload in php. ini;
2. there is a maximum allowed Upload value in php. ini. the default value is 2 MB. It can be changed when necessary;
3. to upload a form, remember to write enctype = "multipart/form-data" in the form label ";
4. the submission method must be post;
5. set the form control of type = "file;
6. check whether the size of the uploaded file, MAX_FILE_SIZE, file type meet the requirements, and the path stored after Upload exists.

You can obtain the file suffix through the uploaded file name, and then rename the file using the timestamp + file suffix. This avoids duplicate names.
You can set the storage directory of the uploaded file and splice it with the file name to form a file path. you can use move_uploaded_file () to complete the process.
Save the file to the specified directory.


52. $ _ FILES is a dimension array? What are the index subscripts of the first and second dimensions? What should I pay attention to when uploading files in batches?
Two-dimensional array. The first dimension is the name of the upload control, and the two-dimensional subscript is name/type/tmp_name/size/error.


53. What are the main functions of the header () function? What should I pay attention to during use?
A:

54. when downloading an object, if the header () function is used?
A: header ("content-type: application/octet-stream; charset = UTF-8"); // What is the difference between UTF-8 and above definition? ,??
Header ("accept-ranges: bytes ");
Header ("accept-length:". filesize ($ filedir. $ filename ));
Header ("content-disposition: attachment; filename =". $ filedir. $ filename );

 

 

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.