Samouczek PHP

Strona główna PHP Wprowadzenie do PHP Instalacja PHP Składnia PHP Komentarze PHP Zmienne PHP PHP Echo / Drukuj Typy danych PHP Ciągi PHP Liczby PHP Matematyka w PHP Stałe PHP Operatory PHP PHP Jeśli...Inne...Elseif Przełącznik PHP Pętle PHP Funkcje PHP Tablice PHP PHP Superglobals PHP RegEx

Formularze PHP

Obsługa formularzy PHP Walidacja formularzy PHP Wymagany formularz PHP Adres URL/e-mail formularza PHP Formularz PHP kompletny

Zaawansowany PHP

Data i godzina w PHP Uwzględnij PHP Obsługa plików PHP Otwórz/odczytaj plik PHP Tworzenie/zapisywanie plików PHP Przesyłanie plików PHP Pliki cookie PHP Sesje PHP Filtry PHP Zaawansowane filtry PHP Funkcje wywołania zwrotnego PHP PHP JSON Wyjątki PHP

PHP OOP

PHP Co to jest OOP Klasy/obiekty PHP Konstruktor PHP Destruktor PHP Modyfikatory dostępu PHP Dziedziczenie PHP Stałe PHP Klasy abstrakcyjne PHP Interfejsy PHP Cechy PHP Metody statyczne PHP Właściwości statyczne PHP Przestrzenie nazw PHP Iterowalne PHP

Baza danych MySQL

Baza danych MySQL Połączenie MySQL Tworzenie bazy danych MySQL Utwórz tabelę MySQL Wstaw dane MySQL Pobierz ostatni identyfikator MySQL Wstaw wiele MySQL Przygotowano MySQL Wybierz dane MySQL MySQL Gdzie Zamów MySQL według Usuń dane MySQL Dane aktualizacji MySQL Dane limitu MySQL

PHP XML

Parsery PHP XML Parser PHP SimpleXML PHP SimpleXML — Get Rozszerzenie PHP XML PHP XML DOM

PHP - AJAX

Wprowadzenie do AJAX AJAX PHP Baza danych AJAX XML AJAX Wyszukiwanie na żywo AJAX Ankieta AJAX

Przykłady PHP

Przykłady PHP Kompilator PHP Quiz PHP Ćwiczenia PHP Certyfikat PHP

Odniesienie do PHP

Przegląd PHP Tablica PHP Kalendarz PHP Data PHP Katalog PHP Błąd PHP Wyjątek PHP System plików PHP Filtr PHP PHP FTP PHP JSON Słowa kluczowe PHP Biblioteka PHP Poczta PHP Matematyka w PHP Różne PHP PHP MySQLi Sieć PHP Kontrola wyjścia PHP PHP RegEx PHP SimpleXML Strumień PHP ciąg PHP Obsługa zmiennych PHP Parser PHP XML Kod pocztowy PHP Strefy czasowe PHP

Funkcja PHP vfprintf()

❮ Odniesienie do ciągów PHP

Przykład

Napisz tekst do pliku tekstowego o nazwie „test.txt”:

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

Wynikiem powyższego kodu będzie:

40

Do pliku „test.txt” zostanie zapisany następujący tekst:

There are 9 million bicycles in Beijing.


Definicja i użycie

Funkcja vfprintf() zapisuje sformatowany ciąg do określonego strumienia wyjściowego (przykład: plik lub baza danych).

W przeciwieństwie do fprintf(), argumenty w vfprintf() są umieszczone w tablicy. Elementy tablicy zostaną wstawione przy znakach procentu (%) w głównym ciągu. Ta funkcja działa „krok po kroku”. Przy pierwszym znaku % wstawiany jest pierwszy element tablicy, przy drugim znaku % wstawiany jest drugi element tablicy itd.

Uwaga: Jeśli jest więcej znaków % niż argumentów, musisz użyć symboli zastępczych. Symbol zastępczy jest wstawiany po znaku % i składa się z numeru argumentu i "\$". Zobacz przykład drugi.

Wskazówka: Powiązane funkcje: fprintf() , printf() , sprintf() , vprintf() i vsprintf() .


Składnia

vfprintf(stream,format,argarray)

Wartości parametrów

Parameter Description
stream Required. Specifies where to write/output the string
format Required. Specifies the string and how to format the variables in it.

Possible format values:

  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Additional format values. These are placed between the % and the letter (example %.2f):

  • + (Forces both + and - in front of numbers. By default, only negative numbers are marked)
  • ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
  • - (Left-justifies the variable value)
  • [0-9] (Specifies the minimum width held of to the variable value)
  • .[0-9] (Specifies the number of decimal digits or maximum string length)

Note: If multiple additional format values are used, they must be in the same order as above.

argarray Required. An array with arguments to be inserted at the % signs in the format string


Szczegóły techniczne

Wartość zwrotu: Zwraca długość napisanego ciągu
Wersja PHP: 5+

Więcej przykładów

Przykład

Napisz tekst do pliku:

<?php
$num1 = 123;
$num2 = 456;
$file = fopen("test.txt","w");
vfprintf($file,"%f%f",array($num1,$num2));
?>

Do pliku „test.txt” zostanie zapisany następujący tekst:

123.000000456.000000

Przykład

Wykorzystanie symboli zastępczych:

<?php
$number = 123;
$file = fopen("test.txt","w");
vfprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",array($number));
?>

Do pliku „test.txt” zostanie zapisany następujący tekst:

With 2 decimals: 123.00
With no decimals: 123

Przykład

Użycie printf() do zademonstrowania wszystkich możliwych wartości formatu:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2

// Note: The format value "%%" returns a percent sign
printf("%%b = %b <br>",$num1); // Binary number
printf("%%c = %c <br>",$char); // The ASCII Character
printf("%%d = %d <br>",$num1); // Signed decimal number
printf("%%d = %d <br>",$num2); // Signed decimal number
printf("%%e = %e <br>",$num1); // Scientific notation (lowercase)
printf("%%E = %E <br>",$num1); // Scientific notation (uppercase)
printf("%%u = %u <br>",$num1); // Unsigned decimal number (positive)
printf("%%u = %u <br>",$num2); // Unsigned decimal number (negative)
printf("%%f = %f <br>",$num1); // Floating-point number (local settings aware)
printf("%%F = %F <br>",$num1); // Floating-point number (not local settings aware)
printf("%%g = %g <br>",$num1); // Shorter of %e and %f
printf("%%G = %G <br>",$num1); // Shorter of %E and %f
printf("%%o = %o <br>",$num1); // Octal number
printf("%%s = %s <br>",$num1); // String
printf("%%x = %x <br>",$num1); // Hexadecimal number (lowercase)
printf("%%X = %X <br>",$num1); // Hexadecimal number (uppercase)
printf("%%+d = %+d <br>",$num1); // Sign specifier (positive)
printf("%%+d = %+d <br>",$num2); // Sign specifier (negative)
?>

❮ Odniesienie do ciągów PHP