In PHP, what are the advantages/disadvantages of sprintf compared with direct concatenation?

Source: Internet
Author: User
Is there any best practice for this question? Or how is your code selected? I personally think the sprintf class Cprintf syntax is more concise. Is there any best practice? Or how is your code selected?

I personally thinksprintfClass CprintfSimpler syntax

Reply content:

Is there any best practice for this question? Or how is your code selected?

I personally thinksprintfClass CprintfSimpler syntax

  • In terms of concept, "." is an operator, which is similar to addition and subtraction operations. "sprintf" is a function.
  • Taking the lotusphp framework I have developed for six years as an example, the point operator is used in more than 150 files, and sprintf is only used in 5 files.

In use cases, sprintf has the following advantages (functional ):

  1. Output special format. For example, an unsigned integer, see Case: https://code.google.com/p/lotusphp/so... (109th rows); and, for example, fill in space or 0 before or after the output string, see the official documentation: http://cn2.php.net/sprintf (example 7)
  2. When multiple variables are merged into a new string, the position of the variable in the new string is defined by the input parameter (instead of the code you are writing that is defined by multiple vertex operators ). Example: https://code.google.com/p/lotusphp/so... (line 83)
  3. After defining a new string template, You need to synthesize strings Based on the template multiple times. If the dot operator is used, redundant code is generated. Reference case (this case has a string template, but not multiple calls, you can imagine): https://code.google.com/p/lotusphp/so...

Sprintf is inferior to vertices, for example, echo 'Hello '. $ _ GET ["username"]; include $ ROOT_PATH. '/lib/db. class. php '; I don't think anyone can simplify the process to use sprintf.

Special Account Application answer
You cannot use the url that is spliced by clicking here in curl. It is easy for new users to get stuck !!!

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.