The PHP sprintf function allows you to convert and place a number of arguments in a single character string. Formatting specifiers allow you to work with characters, strings, integers, and real numbers.
The use of the function is in demand for designing tabular information and creating templates. It can be used for preliminary formalization of the initial data in a specific structure, changing their content or sequence.
PHP function syntax sprintf ()
The result of the function is a string of characters formed in a certain sequence from specific data, the parameters of the function:
- format string;
- list of arguments.
You can use any design and logic for formatting, including HTML tags. Although there are no restrictions on the use of sprintf, the PHP manual defines conversion specifiers to a greater extent for numerical information.
The format element starts with the% character and ends with the specifier character. Most formatting options lie in the field of numerical information. The ability to convert a number to binary and hexadecimal formats is very practical. Here the sprintf functions in PHP are hardly worth writing a replacement.
In the given example, the rule for naming arguments and distinguishing the qualifier x from X is especially highlighted. The correct use of references to a sequence of arguments that are formatted is important for the correct result. Numbering always starts with 1.
The sequence of using links does not matter, but it is important to remember: PHP sprintf considers all% elements (without specifying the argument number) sequentially in the general list. Neither the quantity nor the sequence of% elements specified with specific parameter numbers has any relation to the numbering of the general list.
Description of sprintf example in PHP
If there is no direct possibility to manage the argument as a string, then managing the format string and outputting digital information to the result string is not a problem.
In the first two lines of the example (output of the arguments Arg1, Arg2c and Arg2p), the first argument - the line (position name) is displayed as is. The third argument takes 12 (14) characters in the output. The first line aligns the number on the left with zeros to a total length of 12 characters. After the point, four characters. The second line aligns the number on the right (fractional part) to 14 characters. A sign that you need to perform alignment on the right is the symbol -.
A format string is a regular character string. You can insert calculated expressions into it. In this case, inserts {$ cL} and {$ cR} were used to highlight another number. This made the format string clearer and made it easier to write for all the examples.
Formation string formatting
PHP work in Russian, or the sprintf function, is the work of the interpreter in a natural and convenient environment. Actually, an HTML page with PHP code inserts represents any context in any language. This is not the kind of freedom JavaScript provides in a browser environment.
To display Russian content in PHP, you do not need to code the Cyrillic alphabet, but sometimes you have to use the iconv () conversion function. In any case, everything is accessible and readable.
The text for inserting PHP code into an HTML page is understandable and in demand. Application in character string:
Variable ABC = {$ ABC} units
The insert {$ ABC} matters only once and is also executed only once.
The feature of executing PHP code on a page
The interpreter views the page only once, and only once replaces the {$ ABC} inserts with their values. The programmer can, at his discretion, use the phrase "only once", forcing PHP to repeatedly come close to the desired result.
In any case, once the HTML page is sent to the browser, there is no PHP code in it.
If a page element from a browser initiated contact with the server, it can run a new PHP script. Here the insertions {$ ABC} are very relative, and there is no guarantee of the correct result. But using sprintf in PHP is the dynamic of this possibility.
The developer can not only get the desired performance of such an insert, but also change the line to which it will pass the desired value.
Content Generation Moment
Using the potential of PHP as a means of forming inserts into an HTML page, as a tool for generating the whole page is natural. This is where the site begins. As soon as the page is formed, but in fact the visitor came to the site, then according to modern ideas:
- page cannot be changed;
- the user cannot be missed either .
In fact, the visitor came to the site and received the first response in the form of HTML code. It is completely wrong to change this answer - this means telling the visitor that the page is changing it. But to react to the visitor’s actions adequately and on his initiative the page is obliged.
The arrival of the visitor is the first answer. The visitor’s action is an adequate reaction of the page and its preparation for all the expected next actions of the visitor.
A significant portion of the responsibility for page dynamics at the initiative of the visitor rests with JavaScript, but it only works in the browser, and only AJAX requests can be sent to the server. Each server call can run a PHP script. But the moment of page formation and the moment of reaction to the request differ significantly.
The PHP sprintf function is an ideal tool for dynamically generating a response template and filling it with the desired value.
Changing PHP code at runtime
The idea of code dynamics was born along with the first programming language and today is present in every modern programming language.
Executing code with eval (line of code) today is considered a security breach, but it is as much in demand as it is dangerous.
To change the code during execution, it is not necessary to use the eval () function today. It is rather a quick breakfast, the execution of one or two teams, rather than a practical foundation for building the desired dynamics.
The PHP function sprintf () allows you to design a script template, for example, the code of an object that will be called during a particular visitor action. The prevailing practice is to record the bodies of objects in the form of independent files - an ideal solution for changing them during the operation of the site.
Using tools for working with PHP strings that supplement the functionality of sprintf () allows not only changing the objects used as a result of the actions of a particular visitor, but also transferring this dynamics to work with other visitors: this is the development of the site, the dynamics of its functionality, the accumulation of work experience and knowledge.