PHP Format specifiers


Formatted strings use format specifiers to create the basic structure of the string. Format specifiers - it is a predefined sequence of characters that can be used to determine the type of data to be stored or displayed, as well as how any given value should be formatted, such as precision, padding, etc. Format specifiers usually start with a character percentiles or "%". followed by a character sequence that specifies the data type and the desired format. While iterating over the format, if any format specifier is encountered, the compiler / interpreter understands that there is a corresponding directive whose value must be formatted and used. Consequently, the string may not contain a format specifier at all, but if it has at least the same number of directives it must be resubmitted. If over-specified, some languages ​​simply ignore the unnecessary and allow you to execute them with caution. Below is a brief discussion of the formats and data types that can be specified in PHP. Each is implemented with a preceding percentile character or "%". Value formatting
  • Sign specifier can be used to force a sign (- or +) to be used on a number. By default, only the - displayed on negative numbers. When using this specifier, positive numbers are displayed with a leading +. This can be achieved with the + character and can only be implemented for numeric values. Example,
    % + d // Specify the integer along with it's sign (+ or -). 
  • Padding qualifiercan be used to specify which character will be used to pad the results to any specified line size. By default, spaces are used as indentation. Alternative placeholders can be specified using the single quote or ' prefix. Example,
    % '0d // Pad with 0s to achieve the right length. 
  • The alignment specifiercan be used to specify the alignment of the result, i.e. left or right aligned. This is correct by default. Using the - makes it aligned to the left. Example,
    % -s // Specifies the alignment as left-justified. 
  • The width specifiercan be used to specify the minimum number of characters present in the result itself. You can specify any number indicating the minimum width. Most often used with padding specifier. Example,
    // Specifies there should be at least 5 digits,% '05d // if less, then 0s are filled to get the desired result. 
  • Precisionspecifier can be used to define precision when working with real numbers. Dot or '. ' followed by an optional decimal digit string, which refers to the decimal digits displayed after the decimal fraction. 
    When used in a string, it defines the maximum character limit in the string. 
    % .5f // Defines Real Number Precision. % .2s // Maximum Character to be allowed in a string. 
Data types
  • %: To display%. No directives are required.
  • b: the directive refers to an integer and is displayed as a binary number.
  • c: The directive refers to an integer and is displayed as the corresponding ASCII character.
  • d: directive refers to an integer and is displayed as a decimal number.
  • e: directive refers to scientific notation (e.g. 2.12e + 3).
  • E: Alias ' e & # 39 ;.
  • f: directive refers to a floating point number and is displayed as a real number (language sensitive).
  • F: directive refers to a number with floating point and displayed as a real number (without regard to locale).
  • o: The directive refers to an integer and is displayed as an octal number.
  • s: The directive is processed and displayed as a string.
  • u: The directive refers to an integer and is displayed as an unsigned decimal number.
  • x: The directive refers to an integer and is displayed as a hexadecimal number (in lower case ).
  • X: The directive refers to an integer and is displayed as a hexadecimal number (with capital letters).
The following code illustrates how the various format specifiers work:  
// PHP program to illustrate the work
// different format specifiers 
// Create dummy variables $numValue = 5; $strValue = "GeeksForGeeks" ;  
// Use a sign specifier. printf ( "Signed Number:% + d" , $numValue );  
// Padding and width specifier. printf ( "Padding and Width%' 03d% '03d" , $numValue , $numValue + 10);  
// Exact Qualifier. printf ( "Precision:% .5f% .5s" , $numValue , $strValue );  
// Various data types.
// Integer and percentile. printf ( "Percentage :% d %% " , $numValue );  
// Binary octal and hexadecimal representation. printf ( "Binary:% b Octal:% o Hexadecimal:% x" , $numValue +10, $numValue + 10, $numValue + 10);  
// Introduce the character. printf ( "Character:% c" , $numValue + 60);  
// Lines. printf ( "String:% s" , $strValue );  
// Real numbers. printf ( "RealNumber:% f" , 1 / $numValue );  
// Scientific numerical representation. printf ( "Scientific Representation:% e" , $numValue + 100);  
Signed Number: +5 Padding and Width 005 015 Precision: 5.00000 Geeks Percentage: 5% Binary: 1111 Octal: 17 Hexadecimal: f Character: A String: GeeksForGeeks RealNumber: 0.200000 Scientific Representation: 1.050000e + 2 

Get Solution for free from DataCamp guru