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

autoload
Release: 2023-04-09 21:24:02
Original
1953 people have browsed it

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...)
Copy after login

  • $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). <br/>

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/>
Copy after login

PHP document

<br/>

<?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
?>
Copy after login
text1.php内容:There are 9 Not OK in Are you OK?.
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template