10 PHP programming habits help you find a job, and 10 php programming jobs

Source: Internet
Author: User

10 PHP programming habits help you find a job, and 10 php programming jobs

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 experienced the fun of 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 programmer is very attractive, so 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 out the following code errors:

$ X = new Array ();

$ X [sales] = 60;

$ X [profit] = 20:

Foreach ($ x as $ key = $ value ){

Echo $ key + "" + $ value +"

";

}

}

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 does not cause serious errors, this 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? "

"Instead,"

"?

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, such as "". 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 will be 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 first embodiment of 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.

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.