10 PHP programming habits to help you interview

Source: Internet
Author: User
Tags foreach constant php programming

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] =
    $x [Profit] =:
    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 a variable substitution, escaping, and so on, such as "". 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 quiz, 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 an index that is not enclosed 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? ". "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. With ". PHP "bar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.