PHP: vfprintf() function
The vfprintf() function writes a formatted string to a stream.
vfprintf (file_handle, format, array_args)
|file_handle||When a file is successfully opened by fopen() or fsockopen() it returns a resource ID, which is referred as file handler or file pointer.||Required||Resource|
|format|| Each conversion specification starts with a single percent sign (%) and ends with the following conversion characters.
% - returns a percent sign.
b - the argument is treated as an integer and display it as a binary number.
c - the argument is treated as an integer and display it as a ASCII value.
d - the argument is treated as an integer and display as a signed decimal number.
e - the argument is treated as scientific notation (e.g. 1.2e+2).
E - the argument treated as scientific notation (e.g.1.2E+2).
u - the argument is treated as an integer, and display as an unsigned decimal number.
f- the argument is treated as a float, and display as a floating-point number. (local aware)
F - the argument is treated as a float, and display as a floating-point number (non-locale aware).
g - shorter of %e and %f.
G - shorter of %E and %f.
o- the argument is treated as an integer, and display as an octal number.
s - the argument is treated as string and display as a string.
x - the argument is treated as an integer and display as a hexadecimal number (with lowercase letters).
X - the argument is treated as an integer and display as a hexadecimal number (with uppercase letters).
Optional specification :
Sign specifier : display a sign (+ or -) in front a number. By default a - sign is used in front of a number if it is negative.
Padding character :
Default character is space. An alternate padding character can be specified by prefixing it with a single quote.
Alignment specifier. : - character makes the alignment left-justified. The default is right justified.
Width specifier : An integer number specifies the width of the field.
Precision specifier : The argument specify how many decimal number should be displayed for floating numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string.
|array_args||Display array values as a formatted string according to the specified format.||Required||Array|
The length of the outputted string.
Value Type : Integer
Example of php vfprintf() function
<?php $string1 = 'My Roll No. is :'; $roll_no = 22; $file_handler = fopen('testvf.txt','w'); vfprintf($file_handler, '%s %u', array($string1, $roll_no)); fclose($file_handler); // Let open the file file once again and read the content. $file_name = 'testvf.txt'; $file_handler = fopen($file_name, 'r'); $file_data = fread($file_handler, 1024); fclose($file_handler); echo ($file_data); ?>
My Roll No. is : 22
PHP: Tips of the Day
In PHP, there are two versions of logical AND and OR operators.
|$a and $b||Both $a and $b are true|
|$a && $b||Both $a and $b are true|
|$a or $b||Either $a or $b is true|
|$a || $b||Either $a or $b is true|
Note that the && and || opererators have higher precedence than and and or. See table below:
|Evaluation||Result of $e||Evaluated as|
|$e = false || true||True||$e = (false || true)|
|$e = false or true||False||($e = false) or true|
Because of this it's safer to use && and || instead of and and or.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework