Topic: Find the error in the following code:
Function baz ($y $z) {
$x = new Array ();
$x [ Sales] = 60;
$x [Profit] =:
foreach ($x as $key = $value) {
&nbs p; echo $key + "" + $value + "<BR>";
}
}
How many can you find?
If you find that there are fewer commas in the function argument list, "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 that I find 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.
then let's take a look at 10 PHP programming habits I've written:
1, strings enclosed in single quotes
when you use double quotes to enclose a string, the PHP interpreter makes variable substitutions, escapes, 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, output of the string
which of the following statements do you think is the fastest running?
print Hi My name is $a. I am $b "; The
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 one runs the fastest. 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 an array index
as you can see in the above test, 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 abbreviated form of the START flag
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".