String formatting in PHP with Example -Technoknowledge4k

 String formatting in PHP with Example 

A percent sign (%) is always placed before an input that is anticipated to be used and assessed as a component of the formated strings, followed by the specifiers or rules:

Note:  All specifier , excluding the type specific , are optional

String formatting in PHP with Example -Technoknowledge4k


  • a distinctive indication. Positiv AND negative signals are made obvious by using a plus sign (+). (only negative values are specified by default).
  • a particular cushioning. There is no need to be particular about the default, which is a space. You can also use a zero (0) without any supplementary notation. Should another character be used. it should be preceded with a single quote
  • a specifier alignment The padding is positioned on the left of the string because the default is right-justified. An underscore (_) or dash (-) will set it to left-justified.
  • a width specifier, which is an integer that specifies the minimum number of characters the output must contain. The chosen width less the input's length, when paired with padding, defines how many padded characters will be added.
  • a specifier of precision The number of decimal places that should be output for a float is specified by a period() followed by an integer. When applied to a string, it sets the output's maximum character limit. Specifying a type:
  • %-a literal percent sign , thus would be written %%  to display a percent sign in the formatting string.
  • b-the input should be a integer a binary should be output.
  • The input should be an integer between 0-255 that represents the value of an ASCII byte. The output is the character seen.
  • d-the input should be integer.
  • e-the input should be the scientific notation .
  • f-The input is float.
  • F-The input is float .
  • o-The output is an octal number, and the input is an integer.
  • s-The input is string.
  • x-The output is a hexadecimal number, and the input is an integer.
  • X-The output is a hexadecimal number, and the input is an integer.
Example 

<?php
printf("USA comprise %d states and %d teritories",6,10);
printf("This  is the number of %s as a float (%f) , a binary integer (%b),an octal integer(%o) and a hex integer (%x)";543.21.543.23,543.20,543.20,5433.20);
printf("%d",36); / /Display "36"
printf("%d",-36) // "-36"
printf("%+d",36)// "+36"
echo "</br>";
printf("%+d",-36)/ "-36"
printf(br/)
printf("%04d",-36)// 0012
print"<br/>";
printf("%04d".1234)// "1234"
print"<br/>";
printf("%05d".12345)// "12345"
echo"<br/>";
printf("%10s"."Hello")// "Hello"
echo"<br/>"
print"<br/>";
printf("%`*10s"."Hello")// "*****Hello"
echo"<br/>";
printf("%*-10s"."Hello")// "Hello*****"
echo"<br/>";
printf("%10s*-"."Hello")// "Hello"
echo"<br/>";
printf("%f".123.456)// "123..456000"
echo"<br/>";
printf("%2f".123.456)// "123..45"
echo"<br/>";
printf("%10f".123.456)// "123"
echo"<br/>";
printf("%f".123.456)// "123..456000"
echo"<br/>";
printf("%.0f".123.456)//display "123"
printf("%08.2f".123.456)//"123..456000"
printf("%3s"."Hello")//"hel"
?>

The Sprintf() function


A prepared string is written to a variable by the sprintf() method. The main string will have the arg1, arg3, and ++ arguments added at the percent (%) sign. The function operates "Step-by-Step," inserting arguments at the first and second percent signs, 

Example


<?php
$number = 9;
$str = "bejjing";
$txt = sprint("There are %u million - bicycle in %a . ",$number,,$str");
echo $txt;
?>

The vsprintf() function 

The prepared string is written to a variable via the vsprintf() function. Unlike sprintf(), vsprint() places the arguments in an array, and the array elements are added to the main string after the percent (%) sign.

Example 

<php
$number =9;
$str="Bejiing";
$txt = vsprintf("There are %u million bicycle in %s ,",array($number,$str)");
echo $txt;
?>

The vprintf() function

operates similarly to printf() but accepts an array of arguments as opposed to a variable number. Array values will be shown as a formatted string in accordance with format (which is detailed in the sprintf() documentation).

int vprint( string $format , array $args)

Ex:-

<php
vprintf("%04d-#0d-%02d",explode('-','1997-07-02'));//1997-07-03
?>

The fprintf() function 

write the output in a file
<?php
$number = 9;
$str = "beijing";
$file = fopen("txst.txt","w");
echo fprintf($file,"here are %u million bicycles in %s.".$number,$str");
?>

The following text will be written to the file "test.txt"

These are 9 million bicycles in beijing.

The vfprintf() function

The arguments in vfprint() are stored in an array, unlike fprintf(). At the percent (%) signs in the primary string, the array elements will be added.

<?php
$number = 9;
$str= "Beijing";
$file = fopen("test.txt","w");
echo vfprint($file,"there are %u million bicycle in %s.","array($number,$str));
?>

The following text will be written to the file "test.txt":
there are 9 million bicycles in Beijing.


thanks for visiting our website please feel free to share your valuable opinion for this blog in comment section .

Post a Comment

Previous Post Next Post