The past few weeks have been a rather complicated experience for me. I was one of the big layoffs in our company, but it was a pleasure to experience. I've never been expelled, so it's hard not to think too much. I started browsing the recruiting section, and a full-time PHP programmer's position was fascinating, so I sent a resume and got an interview. During the interview, I talked to the main program staff on the phone, and finally they gave me a set of test questions, one of which was intriguing.
Find out the error of the following code:
?
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 a comma is missing from the list of function arguments, "The new Array () is incorrect, the end of the line is a colon instead of a semicolon, there is no" => "in foreach and the string is concatenated with" + ", congratulations, you've found all the mistakes, you've mastered the basics of PHP programming.
Now I'm going to say how I answered the question. I have also identified the above issues, but I went further. For example, have you found that you don't enclose strings in quotes in an array index? Although this does not cause a serious error, this is a coding error. Also, did you notice that in the Echo line it uses double quotes instead of single quotes? Use the abbreviated form of the PHP start tag? And not using "<br/>" but using "<BR>"?
After I found out the actual error, I added a comment after the problem I had identified above. This is enough to change the answer from "correct" to "sobering", which adds a lot of points to my application, so they decided to hire me. (But I finally turned it down because I liked the tight pace of life and dedicated my PHP skills to my clients rather than a company that dabbled in the telecoms market.) I need a stage to show my skill. )
So let's take a look at 10 PHP programming habits I've written:
1. Use a string enclosed in single quotes
When you use double quotes to enclose a string, the PHP interpreter makes variable substitution, escaping, and so on, such as \ n. If you only want to output a basic string, use single quotes, which will save you some resources. Of course, if you need to replace the variable, you must use double quotes, but in other cases, use single quotes.
2, the output of the string
Which of the following statements do you think is running the fastest?
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 the last one is actually the fastest running speed. Print is slower than echo, slower when you make variable substitutions in a string, and the connection string is slower than a comma connection, and the last sentence is the first habit. Therefore, not only does variable substitution in the string speed up the program, it also makes your code more understandable in any syntax-highlighting editor (the variables are highlighted). Few people know that the parameters of echo can be connected by commas, and that the speed is faster than string concatenation. Finally, using the first habit, the statement is very good.
3. Using single quotes in the array index
As you can see in the above test questions, I pointed out that $x[sales] is technically wrong and that the index should be enclosed, that is, $x[' sales '. This is because PHP will recognize the not-enclosed index as a "naked" string and interpret it as a constant. When the definition of the constant is not found, it is interpreted as a string, so the statement is operational. You can omit this part of the work by enclosing the index, and there will be no error if you want to define constants with this string in the future. I've even heard that it takes about seven times times faster, though I haven't tested it myself. For more discussion on this topic, see the "array of energy and cannot" section of the PHP manual " array ".
4, do not use the initial sign of the abbreviation form
Are you using such symbols? "A;?" is a very bad symbol, and it can cause conflicts with the XML interpreter. And once you publish the code, the user must modify the php. ini file to open support for this symbol. So there is really no reason to use this form. Use "<?php".
5, try not to use regular expressions
Do not use regular expressions (Preg and Ereg series functions) as much as possible when doing regular string operations. Str_replace functions are much faster than preg_replace, and even strtr functions are faster than str_replace. Save these unnecessary troubles, and your boss will thank you.
6, do not use the function in the circular Declaration
The problem is not just in PHP, you can see it in other languages as often:
Bad: for ($i =0; $i <count ($array); $i + +) {...}
Good: $count =count ($array); for ($i =0; $i < $count; $i + +) {...}
That's a good explanation, but a lot of people just want to waste system resources by writing a line less code. If the Count function is used in a circular declaration, it is called once per loop. If you have a lot of cycles, you'll waste a great deal of time.
7. Never use Register_globals and magic quotes
These are two very old functions that might have been a good one at the time (ten years ago), but it does not seem so now. The old version of PHP will open these two features by default when installed, which can cause security vulnerabilities, programming errors, and other problems, such as creating variables only when the user enters data. Now these two features have been discarded, so every programmer should avoid using them. If your past programs have used these two functions, remove them as quickly as possible.
8, must initialize the variable (here "initialization" refers to the "declaration"-Translator Note)
When you need a variable that is not initialized, the PHP interpreter will automatically create a variable, but it is not a good idea to rely on this feature to program. This can cause the program to be rough, or make the code confusing, because you need to find out where the variable started to be created. In addition, incrementing an uninitialized variable is slower than initializing it. So it would be a good idea to initialize the variables.
9, comments on the code
This question has been mentioned many times, but it is not enough for many times. I know that there are some places that don't hire programmers who don't comment on code. After my previous working face, I went through my code with the VP and the interviewer, and when they were impressed by the code comments I made, I learned about my habit. A day later, I got the job.
I know that some people who call themselves PHP masters claim that their code is well written and don't need to add any annotations. In my opinion, these people are rubbish. It is worthwhile to learn the specifications and techniques of annotations, and to familiarize yourself with annotation aids such as phpdocumentor or Doxygen .
10. Follow a programming specification
In this regard, you need to ask your potential boss for an interview, asking what programming specifications they are using. PEAR? Zend? Internal specifications? Mention the programming specifications you're using, whether you created it yourself or the one that is currently prevalent. If there is no good programming specification for PHP's baggy language, then the code will look like a heap of rubbish. Smelly, disgusting rubbish. Some basic specifications include a space specification, a brace match, a naming style, and so on. This is necessary for anyone pursuing high quality code.
Someone said, "I hate the indentation of your 4 spaces." "I'm going to say, what?" Indent with 4 spaces? This is more than a 3-character space with tabs. More importantly, you can customize the indent value of tabs as long as you use an advanced editor than Notepad. So every programmer can look at the code in the way it's used to. Can be set to 4 or set to 0 (if you are a masochist). Anyway, I don't care, but you just can't use the space to indent!
In general, I hope that these programming habits will help you. If you want to make a good impression in an interview, just need some small details.