Smart, how many places can you find? If you find that there is a missing comma in the function parameter list, "new Array()" is incorrect, a colon is used at the end of the line instead of a semicolon, "=>" is not used in foreach and "+" is used to connect strings, Congratulations, you found all the errors and you have mastered the basics of PHP programming. Of course I have also identified the above problems, but I go one step further. For example, have you ever noticed that strings in array indexes are not enclosed in quotes? While this doesn't cause a critical error, it is a coding error. Also, did you notice that in the echo line it uses double quotes instead of single quotes? Using the abbreviated form of the PHP start flag? And instead of using “ After I found the actual error, I added a comment after the question found above. That was enough to move the answer from "correct" to "thought-provoking," which gave my application a lot of points, so they decided to hire me. (But in the end I refused because I like the tight pace of life and dedicate my PHP skills to my customers, rather than a company involved in the telecommunications market. I need a stage to show off my skills.) 10 good habits for PHP programming 1. Use strings enclosed in single quotes When double quotes are used to enclose a string, the PHP interpreter will perform variable substitution, escaping and other operations on it, such as " ". If you just want to output a basic string, use single quotes, which will save some resources. Of course, if you need to perform variable substitution, you must use double quotes, but in other cases, use single quotes. 2, String output Which of the following statements do you think runs the fastest?
Maybe this seems strange, but the last one actually runs the fastest. Print is slower than echo, it is slower when replacing variables in a string, and concatenating strings is slower than concatenating with commas. The last sentence is the embodiment of the first habit. So, not doing variable substitutions in strings will not only make your program run faster, it will also make your code more readable in any editor with syntax highlighting (variables will be highlighted). Few people know that echo parameters can be concatenated with commas, and the speed is faster than string concatenation. Finally, if you use the first habit, this statement will be very good. 3. Use single quotes in array index As you can see in the test question above, I pointed out that $x[sales] is technically wrong and the index should be bracketed, i.e. $x['sales']. This is because PHP recognizes an unquoted index as a "naked" string and interprets it as a constant. When the definition of the constant cannot be found, it is interpreted as a string, so this statement is runnable. Enclosing the index can save this part of the work, and there will be no errors if you happen to use this string to define a constant in the future. I've even heard that doing this makes it about seven times faster, although I haven't tested it myself. For more discussion on this topic, see the section "What Arrays Can and Can't Do" in the "Arrays" chapter of the PHP manual. 4. Do not use the abbreviation of the start symbol Are you using such symbols? "" is a very bad symbol and can cause conflicts with XML interpreters. And once you publish this code, users must modify the php.ini file to turn on support for this symbol. So there's really no reason to use this form. Use " 5. Try not to use regular expressions When performing regular string operations, try not to use regular expressions (preg and ereg series functions) as much as possible. The str_replace function is much faster than preg_replace, and even the strtr function is faster than str_replace. Save yourself the trouble and your boss will thank you. 6. Don’t use functions in loop declarations This problem does not only appear in PHP, you can often see it in codes in other languages:
Difference: for($i=0;$i This should be easy to explain, but many people just want to write one less line of code and waste system resources. If the count function is used in the loop declaration, it will be called once for each loop. If you have a lot of loops, it will waste a lot of time. 7. Never use register_globals and magic quotes These are two very old features that may have been a good approach at the time (ten years ago), but it seems not to be the case now. Older versions of PHP will turn on these two functions by default when installed, which can cause security holes, programming errors and other problems. For example, variables will only be created when the user enters data. Both features are now deprecated and should be avoided by every programmer. If your past programs have used these two features, remove them as soon as possible. 8. Be sure to initialize variables ("Initialization" here refers to "declaration" - translator's note) When an uninitialized variable is needed, the PHP interpreter will automatically create a variable, but it is not a good idea to rely on this feature for programming. This can make the program crude or confusing because you need to figure out where the variable was created. Additionally, incrementing an uninitialized variable is slower than an initialized variable. So it would be a good idea to initialize variables. 9. Comment the code This question has been raised many times, but no amount of times is enough. I know some places won't hire programmers who don't comment their code. After a previous job interview, I went through the code I wrote with the vice president and interviewer. When they were impressed by the code comments I made, they also learned about this habit of mine. One day later, I got the job. I know some people who call themselves PHP masters claim that their code is well written and does not need to add any comments. In my opinion, these people are trash. It is worthwhile to learn the specifications and techniques for writing comments and become familiar with comment assistance software such as phpDocumentor or Doxygen. 10. Follow a programming specification What programming standards to use. PEAR? Zend? Internal regulations? Mention the programming convention you are using, whether it is one of your own creation or one that is currently popular. For a loose language like PHP, if there is no good programming specification, then the code will look like a bunch of garbage. Some basic specifications include space specifications, bracket matching, naming style, etc. This is a must for anyone who pursues high-quality code. Someone said: "I hate your 4-space indentation." I was going to say, what? Use 4 spaces for indentation? This takes up 3 characters more space than using tabs. More importantly, you can customize the tab indentation value as long as you use an editor more advanced than Notepad. So every programmer can look at the code in the way they're most comfortable with. Set to 4 when possible, or 0 if you're a masochist. I don't care anyway, but you just can't use spaces for indentation! |