Php Comprehensive Exercises

Source: Internet
Author: User
Tags echo date 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 = '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:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
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 (such as <script> alert (111); </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.