The position of a full-time PHP programmer is very attractive. if you sent your resume and got an interview opportunity. During the interview, you chatted with the main programmers on the phone and finally they gave me a set of test questions, one of which was intriguing.
The position of a full-time PHP programmer is very attractive. if you sent your resume and got an interview opportunity. During the interview, you chatted with the main programmers on the phone and finally they gave me a set of test questions, one of which was intriguing.
Find out the following code errors:
[Php]
$ X = new Array ();
$ X [sales] = 60;
$ X [profit] = 20:
Foreach ($ x as $ key = $ value ){
Echo $ key + "" + $ value +"
";
}
}
[/Php]
How many can you find?
If you find that comma, "new Array () is missing in the function parameter list () "It is incorrect. a colon instead of a semicolon is used at the end of the line," => "is not used in foreach, and" + "is used to connect the string. Congratulations, you have found all the errors, you have mastered the basics of PHP programming.
Now I want to explain how I answered this question. Of course I also found out the above problems, but I went further. For example, have you found that strings are not enclosed by quotation marks in array indexes? Although this will not cause a serious error, it is an encoding error. In addition, have you noticed that double quotation marks instead of single quotation marks are used in the echo line? Is the PHP start sign abbreviated? "
"Instead,"
"?
After finding out the actual error, I added a comment after the problem found above. This is enough to change the answer from "correct" to "thought-provoking", which also adds a lot of points to my application.
Next, let's take a look at the 10 PHP programming habits that are very useful for you to work with PHP:
1. a string enclosed in single quotes
When double quotation marks are used to enclose strings, the PHP interpreter replaces and escapes the variables, for example, "\ n ". If you only want to output a basic string, use single quotes to save some resources. Of course, if you need to replace the variable, you must use double quotation marks, but in other cases, you should still use single quotation marks.
2. string output
Which of the following statements can run at the fastest speed?
Print "Hi my name is $ a. I am $ B ";
Echo "Hi my name is $ a. I am $ B ";
Echo "Hi my name is". $ a. ". I am". $ B;
Echo "Hi my name is", $ a, ". I am", $ B;
Echo 'Hi my name is ', $ a,'. I am', $ B;
This may seem strange, but in fact the last line is the fastest. Print is slower than echo, and it is slower to replace the variable in the string, and the connection string is slower than using a comma to connect. The last sentence is the embodiment of the first habit. Therefore, replacing variables without strings will not only speed up the program running, but also make your code more understandable in any syntax highlighted editor (variables will be highlighted ). Few people know that echo parameters can be connected with commas, and the speed is faster than string connection. The first habit is used, so this statement is very good.
3. use single quotes in array indexes
As you have seen in the above test questions, I pointed out that $ x [sales] is strictly incorrect. indexes should be included, that is, $ x ['sales']. This is because PHP identifies an unenclosed index as a "bare" string and interprets it as a constant. When the definition of the constant is not found, it is interpreted as a string, so this statement can be run. Excluding indexes can save this part of work. if you want to use this string to define constants in the future, there will be no errors. I have even heard that this is about seven times faster, although I have not personally tested it.