NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - printf

Trieda

Koreň \ Bez triedy

Metóda - printf

(PHP 4, PHP 5, PHP 7)

The printf() function formats an input and immediately prints it.

When formatting the input we should not forget that special characters are not counted only as a single character! This also applies to the output.

Procedurálne

  • function printf (string $format, mixed $args, mixed $...) : int

Parametre

NázovDátový typPredvolená hodnotaPopis
$formatstring

The format of a string. Ordinary characters described below represent variables which must be passed in the exact order after a percent sign (%).

  1. An optional parameter. It is a sign character (- or +) specifying if the number must have the sign character attached. The default character is -; that means only negative numbers have the sign. Otherwise, if the + sign is added, the numbers have always the sign character.
  2. An optional character used in case of padding the result (e.g. if the string is not long enough). Space or 0 can be used as well as any character preceded by an apostrophe (').
  3. A dash (-) that says how a string is padded. If the dash is added, the short string is padded from the right (the default is the left).
  4. An optional number specifying the minimum length of a string.
  5. An optional parameter with a number after a period (.). It defines how many decimal digits a number will have (they are either cut off or zeros are added). It can also be used for strings where the maximum length is then given.
  6. A required parameter specifying what type the argument data should be treated as. Possible types:
  • % - a percent character. No argument is needed.
  • b - converts an integer to a binary number.
  • c - converts an integer to an ASCII character.
  • d - an integer.
  • e - the value is presented in the scientific notation (e.g. 1.2e+2).
  • E - the same as %e but uses the uppercase letter instead (e.g. 1.2E+2).
  • f - a decimal number (locale aware).
  • F - a decimal number (non-locale aware).
  • g - shorter of %e and %f.
  • G - shorter of %E and %f.
  • o - converts an integer to an octal number.
  • s - a string.
  • u - converts an integer to an unsigned number.
  • x - converts an integer to a hexadecimal number (lowercase letters).
  • X - converts an integer to a hexadecimal number (uppercase letters).
$argsmixed

Arguments representing variables in the format string.

$...mixed

Arguments representing variables in the format string.

Mávratovej hodnoty

Vracia: int

The length of the outputted string.

Príklady

<?php
    printf("Hello %s!", "world");

We can also add more parameters when they are inserted in the specified order:

<?php
    printf("%s were %d bananas lying", "There", 5);

We can also change the order:

<?php
    printf('%2$s were %1$d bananas lying', 5, "There");

It is also possible to add the selected character to numbers to a given length:

<?php
    printf("%'.9d", 534);
    echo ('<br />');
    printf("%09d", 534);

And it also applies to texts:

<?php
    echo ('<pre>');
    printf("[%15s]", 'Hello world!');
    echo ('<br />');
    printf("[%015s]", 'Hello world!');
    echo ('<br />');
    printf("[%-15s]", 'Hello world!');
    echo ('<br />');
    printf("[%.15s]", 'Hello world!');
    echo ('<br />');
    printf("[%.15s]", 'Hello beautiful world!');

Súvisiace manuály

        Aktivity