The past few weeks have been quite complex for me. Our company has laid a lot of layoffs. I am one of them, but I have enjoyed it. I have never been fired, so it is hard to think too much. I started to browse the recruitment section, a full-time PHP Program I sent my resume and got an interview. During the interview, I chatted with the main programmers on the phone and finally gave me a set of test questions, one of which was intriguing.
Find the followingCodeErrors:
<?
Function Baz ($ y $ Z ){
$ X = new array ();
$ X [sales] = 60;
$ X [profit] = 20:
Foreach ($ X as $ key = $ value ){
Echo $ key + "" + $ value + "<br> ";
}
}
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? "<Br/>" is not used, but "<br> "?
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, so they decided to hire me. (But in the end I refused, because I like the Compact pace of life and dedicate my PHP skills to my customers, not a company involved in the telecom market. I need a stage to show my skills .)
Next let's take a look at the 10 PHP programming habits I have written:
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. For more information about this topic, see the "Energy and Energy of arrays" section in the "array" Chapter of the PHP manual.
4. Do not use the abbreviated form of the Start sign
Are you using this symbol? "<?" Is a very bad symbol, it will cause conflicts with the XML interpreter. Once you have released the code, you must modify the php. ini file to enable support for this symbol. So there is no reason to use this form. Use "<? PHP.
5. Try not to use regular expressions
Do not use regular expressions (preg and ereg functions) When performing regular string operations ). The str_replace function is much faster than the preg_replace function, and even the strtr function is faster than the str_replace function. Save unnecessary trouble. Your boss will thank you.
6. Do not use functions in loop Declaration
This problem occurs not only in PHP, but also in code in other languages:
Difference: For ($ I = 0; $ I <count ($ array); $ I ++ ){...}
Good: $ COUNT = count ($ array); For ($ I = 0; $ I <$ count; $ I ++ ){...}
This is a good explanation, but many people waste system resources to write less code. If the count function is used in the loop declaration, each loop is called once. If you have a large number of cycles, it will waste a lot of time.
7. Never use register_globals or magic quotes
These are two very old functions. At that time (10 years ago) it may be a good method, but it does not seem so now. The old version of PHP will enable these two features by default during installation, which will cause security vulnerabilities, programming errors and other problems, such as creating variables only when users enter data. These two functions are now abandoned, so every programmer should avoid using them. If your previous programs use these two functions, remove them as soon as possible.
8. You must initialize the variable.("Initialization" here refers to "declaration"-Translator's note)
When no initialization variable is required, the PHP interpreter will automatically create a variable, but programming with this feature is not a good idea. This can cause program roughness or code to become confusing, because you need to find out where the variable was created. In addition, incremental operations on a variable without initialization are slower than initialization. So initialization of variables is a good idea.
9. annotate the code
This problem has been raised many times, but it is not enough for many times. I know that programmers who do not comment on Code are not hired in some places. After my previous job interview, I browsed the code I wrote with the vice president and interviewer. When they were impressed by the comments on the code I made, they also learned about my habits. One day later, I got the job.
I know that some people who call themselves PHP masters claim that their code is well written and do not need to add any comments. In my opinion, these people are all rubbish. It is worthwhile to learn the specifications and skills for writing comments and familiarize yourself with annotation auxiliary software such as php‑entor or doxygen.
10. follow a programming Specification
In this regard, you need to ask your potential boss during the interview and ask what programming specifications they are using. Pear? Zend? Internal specifications? You need to mention the programming specifications you are using, whether it's your own, or a popular one. For PHP, a loose language, without a good programming specification, the code will look like a pile of garbage. Smelly, disgusting spam. Some basic specifications include the space specification, matching with parentheses, and naming style. This is required for anyone pursuing high-quality code.
Someone said, "I hate the indentation of four spaces ." What should I say? Use four spaces to indent? This is a three-character space occupied by tabs. More importantly, as long as you use an editor that is more advanced than notepad, you can customize the indentation value of tabs. So every programmer can look at the code in the way he is most used. You can set it to 4 or 0 (if you are a masochistic user ). I don't care about it, but you just can't indent it with spaces!
In general, I hope these programming habits can help you. If you want to make a good impression in the interview, you only need some small details.