Home  >  Article  >  Backend Development  >  Analysis of the fprintf() function in PHP (with detailed code explanation)

Analysis of the fprintf() function in PHP (with detailed code explanation)

autoload
autoloadOriginal
2021-04-19 11:28:381962browse

Analysis of the fprintf() function in PHP (with detailed code explanation)

File writing and reading are essential capabilities for various languages, PHP has built-in# The ##fprintf() function can write the formatted string into the stream. This article will take you to take a look at how to use the fprintf() function to modify the data. Write.

1. Syntax:

fprintf(resource $handle, string $format, mixed $arg1, $atg2...)

  • $handle: Required, specifies where to write/output the string .

  • #$format: Required, specifies the string and how to format the variables in it.

  • $arg1 : Required, specifies the parameter inserted into the first % symbol in the format string.

  • #$arg2 : Optional, specifies the parameters inserted into the second % symbol in the format string.

  • #$argn : Optional, specifies the parameter inserted at the nth % symbol in the format string.

  • Return value: Returns the length of the written string (

    int).

Note: If there are more

% symbols than $arg arguments, placeholders must be used. The placeholder is inserted after the % symbol and consists of numbers followed by "\$".

2. Code example:

Create a new text1.text with empty content

<br/>

PHP document


<?php
$number = 9;
$str = "Are you OK?";
$file = fopen("test1.txt","w");
echo fprintf($file,"There are %u Not OK in %s.",$number,$str);
//输出:34
?>
text1.php内容:There are 9 Not OK in Are you OK?.

Recommended: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of Analysis of the fprintf() function in PHP (with detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn