Home > Article > Backend Development > Comprehensive review questions for PHP_PHP tutorial
1. How to define variables? How to check if a variable is defined? How to delete a variable? Function to determine whether a variable is empty?
isset()
unset()
empty()
2. What is a variable variable?
The variable name of a variable can be set and used dynamically.
$a = 'hello' , $$a = 'world', ${$a}=hello world
3. What are the methods of variable assignment?
1) Direct assignment
2) Assignment between variables
3) Reference assignment
4. What is the difference between quoting and copying?
Copying is to copy the contents of the original variable. The copied variable and the original variable use their own memory and do not interfere with each other.
Reference is equivalent to an alias of a variable, which actually means accessing the same variable content with different names. When you change the value of one variable, the other one also changes.
5. What are the basic data types of variables in php?
PHP supports 8 primitive data types.
Includes:
Four scalar types (boolean, integer, float/double, string)
Two composite types (array, object)
Two special types (resource, NULL)
6. When other types are converted to boolean types, which ones are considered false?
Boolean value false, integer value 0, floating point value 0.0, blank string, string '0', empty array, special data type NULL, no variable set.
Under what circumstances does the empty() function return true?
boolean false,
Integer value 0,
Floating point value 0.0,
Blank string,
String '0',
array() empty array,
Special data type NULL,
Object without any properties,
There is no assigned variable.
7. If a variable $a is defined, but no initial value is assigned
So $a==0?
$a==false?
$a==''?
$a==NULL?
$a===NULL? Answer: echo=>Nothing, var_dump=>NULL
empty($b)==true? ———————————— echo=>1 , var_dump=>bool(true)
What would be the output of $a++ at this time? ———————— echo=>Nothing, var_dump=>NULL
What is the output of ++$a? —————————— echo=>1 , var_dump=>int(1)
8. How to convert a string into an integer? How many methods are there? How to achieve 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 are constants defined? How to check whether a constant is defined? What data types can the value of a constant be?
define()//Define constant, defined()//Check whether the constant is defined
The value of a constant can only be scalar type data.
11. Constants are divided into system built-in constants and custom constants. Please name the most common system built-in constants?
__FILE__ , __LINE__ , PHP_OS , PHP_VERSION
12. If two identical constants are defined, which one will work, the former or the latter?
The former works because a constant once defined cannot be redefined or undefined.
13. What are the differences between constants and variables?
1) There is no $ sign before the constant;
2) Constants can only be defined through define(), not assignment statements;
3) Constants can be defined and accessed anywhere, while variables are divided into global and local;
4) Once defined, constants cannot be redefined or undefined, while variables are redefined through assignment;
5) The value of a constant can only be scalar data, while the database type of a variable has 8 primitive data types.
14. What are the several predefined global array variables commonly used in PHP?
There are 9 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) The information for connecting to the database is defined as constants, such as the user name, password, database name, and host name of the database server;
2) Define part of the site's path as a constant, such as the web absolute path, smarty installation path, model, view or controller folder path;
3) Public information of the website, such as website name, website keywords and other information.
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? Are function names case sensitive?
1) Use the function keyword;
2) Function naming rules are the same as variables, starting with a letter or underscore, not a number;
3) Function names are not case-sensitive;
4) The function name cannot use the function name that has been declared or built by the system.
18. What is variable visibility or variable scope?
It 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 called directly within a function?
Local variables are variables defined within a function, and their scope is the function in which they are located. If there is a variable with the same name as the local variable outside the function,
The program will think that they are two completely different variables. When exiting the function, the local variables are cleared at the same time.
Global variables are variables defined outside all functions. Their scope is the entire php file, but they cannot be used inside user-defined functions.
If you must use global variables inside a user-defined function, you need to use the global keyword declaration.
That is to say, if you add gobal before the variable in the function, then the global variable can be accessed inside the function,
Not only can you use this global variable to perform operations, but you can also reassign this global variable.
Global variables can also be called using $GLOBALS['var'].
20. How to use the global keyword? How to use the predefined global variable array $GLOBALS?
21. What are static variables?
If a variable defined within a function is declared with the keyword static before it, then the variable is a static variable.
Generally, after the function call ends, the data stored in the variables within the function will be cleared and the memory space occupied will be released. When using static variables,
This variable will be initialized when the function is called for the first time. After initialization, the variable will not be cleared. When the function is called again, this static variable
It is no longer initialized, but can save the value after the last function execution. We can say that static variables are shared between all calls to the function.
22. What are the ways to pass parameters to functions in php? What's the difference between the two?
Pass by value and pass by address (or pass by reference)
(1) Pass by value: The variable to be passed is stored in a different space than the variable passed to the function. So in the function body,
Modifications made to the variable value will not affect the original variable value.
(2) Pass by address: Use the & symbol to indicate that the 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 storage block address of the value or variable is imported into the function, so the variable in the function body and the variable in the main program are in memory
are the same. Modifications made to the function body directly affect the value of the variable outside the function body.
23. What is a recursive function? How to make a recursive call?
A recursive function is actually a function that calls itself, but it must meet the following two conditions:
1) Each time it calls itself, it must be closer to the final result;
2) There must be a definite recursion termination condition that will not cause an infinite loop.
Example:
In actual work, it is often used when traversing folders.
If there is an example where you want to get all the files under the directory windows, then first traverse the windows directory. If you find that there are folders in it, then you will call yourself and continue to search downwards, and so on,
Until there are no more folders traversed, this means that all files have been traversed.
24. Determine whether a function exists?
function_exists( string $function_name ) Returns true if it exists, false if it does not exist.
25. What is the difference between func() and @func()?
The second function call will not report an error if it fails, but the first one will report an error
26. What are the usage and differences between include() and require() functions? What about include_once() and require_once()?
The error levels of include and require are different
include_once() and require_once() must determine whether it has been imported before loading
27. Tell me the difference between prefix++ and post++?
Prefix ++ first increments the variable by 1 and then assigns the value to the original variable;
Postfix ++ first returns the current value of the variable, and then increases the current value of the variable by 1.
28. What is the difference between the string operator "." and the arithmetic operator "+"?
When . is used between "a" and "b", it is considered a hyphen. If there is a + between the two, PHP will consider it to be an operation.
1) If the strings on both sides of the + sign are composed of numbers, the strings will be automatically converted to integers;
2) If there are pure letters on both sides of the + sign, then 0 will be output;
3) If the string on both sides of the + sign starts with a number, then the number at the beginning of the string will be intercepted and then operated.
29. What is the ternary (or ternary) operator?
Chooses one of two expressions based on the result of the other expressions.
For example: ($a==true) ? 'good':'bad';
30. What are the control flow statements?
1: Three program structures: sequential structure, branch structure, and loop structure
2: Branch: if/esle/esleif/ switch/case/default
3: Switch needs attention:
The constants in the case clause can be integers, string constants, or constant expressions, and are not allowed to be variables.
In the same switch clause, the case values cannot be the same, otherwise only the value in the first occurrence of the case can be obtained.
4: Loop for while do...while
do...while must be followed by a semicolon.
The difference between while and do...while
5: The difference between break and continue.
break can terminate the loop.
continue is not as powerful as break. It can only terminate this loop and enter the next loop.
31. What is the concept of array? What are the two types of arrays based on indexes, and how to distinguish them? What are the two ways of assigning values to an array?
An array is a variable (composite variable) that can store a set or series of values
Index array (index value is a number, starting from 0) and associative array (with string as the index value)
What are the two ways of assigning values to arrays?
There are two main ways to declare arrays.
1. Declare the array through the array() function;
You can define the index and value separately through key=>value, or you can not define the index subscript of the array and only give the element value of the array.
2. Directly assign values to array elements without calling the array() function. For example:
$arr[0] = 1;
$arr[1] = 2;
Special note:
Array subscripts that are string values equivalent to integers (but not starting with 0) are treated as integers.
For example: $array[3] and $array['3'] refer to the same element, while $array['03'] refers to another element.
32. How to traverse an array?
①for loop
②Foreach loop, this is the most commonly used traversal method. The usage is as follows: foreach($arr as $key=>$value){}
③List each and while are combined to loop
33. How does the pointer point when foreaching an array? How does the pointer point when list()/each()/while() loops through the array?
When foreach starts executing, the pointer inside the array will automatically point to the first element. Because foreach operates on a copy of the specified array, not the array itself.
After each() an array, the array pointer will stay at the next unit in the array or at the last unit when it reaches the end of the array. If you want to use each() to traverse the array again, you must use reset().
reset() rewinds the array's internal pointer to the first element and returns the value of the first array element.
34. How to calculate the length of an array (or count the number of all elements in the array)? How to get the length of a string?
count() -- Count the number of elements in an array.
You can use count (array name) or count (array name, 1). If there is a second parameter and it is the number 1, it means recursively counting the number of array elements.
If the second parameter is the number 0, it is equivalent to the count() function with only one parameter.
sizeof() -- alias of count()
String: strlen(), mb_strlen();
35. What are the common functions related to arrays?
1) count -- (sizeof alias) - Count the number of cells 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, any other type has only one unit. The default value of $mode is 0. 1 turns on recursively counting the array
2) in_array ( mixed $needle , array $haystack [, bool $strict ] ) — Check whether a certain value exists in the array.
If needle is a string, the comparison is case-sensitive.
If the value of the third parameter strict is TRUE, the in_array() function will also check whether the type of needle is the same as that in haystack.
3) array_merge(array $array1 [, array $array2 [, array $... ]] ) merges the cells of one or more arrays, and the values in one array are appended to the previous array. Returns the resulting array.
Special note: If the input array has the same string key name, the value after the key name will overwrite the previous value. However, if the array contains numeric keys, the subsequent values will not overwrite the original values but will be appended to them.
If only an array is given and the array is numerically indexed, the key names are re-indexed in a contiguous manner
4) Conversion between array and string
(1)explode ( string $separator , string $string [, int $limit ] ) uses a separator character to separate a string.
(2)implode ( string $glue , array $arr ) uses a connector to connect each unit in the array into a string.
join is an alias of implode
5) sort(array &$array [, int $sort_flags]) — Sort the array by value. When this function ends, the array cells will be rearranged from lowest to 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 associative array merge, if the key names of the arrays are the same, then the latter value will overwrite the former; if it is a numeric index array merge, it will not be overwritten, but
The latter is appended to the former.
"+"->Use array addition operation. Different from array_merge(), the addition operation discards the values with the same key name, whether it is an associative array or a numerical index array,
That is, only the element in which the key name appears for the first time is retained, and subsequent elements with the same key name will not be added.
37. What is the difference between single quotes and double quotes when defining a string?
38. What are the differences between echo(), print() and print_r()?
(1) echo is a syntax, Output one or more strings, no return value;
(2)print is a function and cannot output arrays and objects. Output a string, print has a return value;
(3)print_r is a function that can output an array. print_r is an interesting function, it can output string, int, float,
array, object, etc., will be represented by a structure when outputting array, and print_r will return true when the output is successful; and print_r can be passed print_r($str,true), so that print_r does not output and returns the value after print_r processing. In addition, for echo and print, echo is basically used because its efficiency is higher than print.
39. What are the string processing functions according to functional classification? What do these functions do?
A. String output function
(1)echo $a,$b,$c...; is a language structure, not a real function.
(2)print($a) This function outputs a string. Returns 1 if successful, 0 if failed
(3)print_r($a)
(4)var_dump($a); can output type, length, value
B. Function to remove spaces from the beginning and end of a string: trim ltrim rtrim (alias: chop) Using the second parameter, you can also remove specified characters.
C. Escape string function: addslashes()
D. Function to get the length of a string: strlen()
E. Function to intercept the length of a string: substr()
F. Retrieve string functions: strstr(), strpos()
G. Replace string function: str_replace()
40. Please give the correct answer to the following question?
1).$arr = array('james', 'tom', 'symfony'); Please split the value of the $arr array with ',' and merge it into a string for output?
echo implode(‘,’,$arr);
2).$str = ‘jack, james, tom, symfony’; Please split $str with ‘,’ and put the split value into the $arr array?
$arr = explode(‘,’,$str);
3).$arr = array(3,7,2,1,’d’,’abc’); Please sort $arr from large to small and keep its key value unchanged?
arsort($arr); print_r($arr);
4).$mail = “gaofei@163.com”; Please take out the domain of this email (163.com) and print it. How many ways can you write at most?
echo strstr($mail,'163');
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 off the last comma in this string?
6). What are the functions for obtaining random numbers? Which execution speed is faster, mt_rand() or rand()?
41. The page characters are garbled, how to solve it?
1. First consider whether the current file has a character set set. Check whether charset is written in the meta tag. If it is a php page, you can also 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 (that is, charset) is set, then determine whether the encoding format saved in the current file is consistent with the character set set on the page,
The two must remain unified;
3. If it involves extracting data from the database, then determine whether the character set when querying the database is consistent with the character set set on the current page. The two must be unified,
For example: mysql_query("set names utf8").
42. What are regular expressions? What are the commonly used functions related to regular expressions in PHP? Please write a regular expression for email, Chinese mobile phone number and landline number?
Regular expressions are a grammatical rule used to describe character arrangement patterns. Regular expressions are also called pattern expressions.
In website development, regular expressions are most commonly used for client-side validation before form submission information.
For example, verify whether the user name is entered correctly, whether the password input meets the requirements, and whether the input of email, mobile phone number and other information is legal.
In PHP, regular expressions are mainly used for string splitting, matching, search and replacement operations.
preg series functions can handle it. Specifically, they are as follows:
string preg_quote ( string str [, string delimiter] )
Escape regular expression characters Special characters for regular expressions include: . \ + * ? [ ^ ] $ ( ) { } = ! < > | :.
preg_replace -- Perform regular expression search and replacement
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
preg_replace_callback -- Use callback function to perform regular expression search and replacement
mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )
preg_split -- Split string using regular expression
array preg_split ( string pattern, string subject [, int limit [, int flags]] )
43. Which function should be used if you want to filter out all html tags in a certain string?
44. What are the differences in the use of preg_replace() and str_ireplace()? How to use preg_split() and split() functions?
45. What are the main functions for obtaining the current timestamp? Use PHP to print out today's time in the format of 2010-12-10 22:21:21?
Use PHP to print out the time of the previous day in the format of 2010-12-10 22:21:21? How to turn 2010-12-25 10:30:25 into 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("2009-9-2 10:30:25");//becomes unix timestamp
echo date("Y-m-d H:i:s",$unix_time);//Format to normal time format
46. When using get to pass values in the URL, if the Chinese characters appear garbled, which function should be used to encode the Chinese characters?
When the user submits data in the website form, in order to prevent script attacks (such as the user inputting <script>alert (111); </script>), how should the PHP side handle the data when it receives it?
Use urlencode() to encode Chinese and urldecode() to decode.
Use htmlspecialchars($_POST[‘title’]) to filter form parameters to avoid script attacks.
47. What are the steps to connect to the database? What data type is the return value of each step? In particular, what data type does mysql_query() return?
48. What are the differences between mysql_fetch_row() and mysql_fetch_assoc() and mysql_fetch_array?
The first one returns a row in the result set as an index array, the second one returns an associative array, and the third one can return either an index array or an associative array, depending on its second parameter MYSQL_BOTH MYSQL_NUM MYSQL_ASSOC defaults to MYSQL_BOTH
$sql =”select * from table1”;
$result = mysql_query($sql);
mysql_fetch_array($result, MYSQL_NUM);
49. Please tell me the functions you have learned so far that return resources?
Answer: mysql_connect();
mysql_query(); Only when the select is successful, the resource will be returned. If it fails, it will return FALSE
fopen();
50. What are the functions for opening and closing files? What are the functions of file reading and writing? Which function is used to delete files?
Which function is used to determine whether a file exists? Which function is used to create a new directory?
51. What details should be paid attention to when uploading files? How to save files to a specified directory? How to avoid the problem of uploading files with duplicate names?
1. First, you need to enable file upload in php.ini;
2. There is a maximum allowed upload value in php.ini, the default is 2MB. Can be changed if necessary;
3. When uploading a form, be sure to write enctype="multipart/form-data" in the form tag;
4. The submission method must be post;
5. Set the form control with type="file";
6. Pay attention to whether the size of the uploaded file MAX_FILE_SIZE, the file type meets the requirements, and whether the path where the file is stored after uploading exists.
You can get the file suffix from the uploaded file name, and then rename the file using the timestamp + file suffix, thus avoiding duplicate names.
You can set the saving directory of the uploaded file yourself, and combine it with the file name to form a file path. Use move_uploaded_file() to complete
Save the file to the specified directory.
52. How many dimensions is $_FILES? What are the index subscripts for 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 subscripts are name/type/tmp_name/size/error.
53. What are the main functions of the header() function? What should you pay attention to during use?
Answer:
54. How to use the header() function when downloading files?
Answer: header("content-type: application/octet-stream;charset=UTF-8"); //What is the difference between adding utf-8 here and defining it above? ,? ?
header("accept-ranges: bytes");
header("accept-length: ".filesize($filedir.$filename));
header("content-disposition: attachment; filename=".$filedir.$filename);