PHP vprintf() 函數(shù)
PHP vprintf() 函數(shù)
實(shí)例
輸出格式化的字符串:
<?php
$number = 9;
$str = "Beijing";
vprintf("There are %u million bicycles in %s.",array($number,$str));
?>
$number = 9;
$str = "Beijing";
vprintf("There are %u million bicycles in %s.",array($number,$str));
?>
運(yùn)行實(shí)例 ?
定義和用法
vprintf() 函數(shù)輸出格式化的字符串。
與 printf() 不同,vprintf() 中的參數(shù)位于數(shù)組中。數(shù)組元素將被插入到主字符串中的百分號(hào)(%)符號(hào)處。該函數(shù)是逐步執(zhí)行的。在第一個(gè) % 符號(hào)處,插入第一個(gè)數(shù)組元素,在第二個(gè) % 符號(hào)處,插入第二個(gè)數(shù)組元素,依此類推。
注釋:如果 % 符號(hào)多于 arg 參數(shù),則您必須使用占位符。占位符被插入到 % 符號(hào)之后,由數(shù)字和 "\$" 組成。請(qǐng)參見(jiàn)實(shí)例 2。
提示:相關(guān)函數(shù):sprintf()、 printf()、 vsprintf()、 fprintf() 和 vfprintf()
語(yǔ)法
vprintf(format,argarray)
參數(shù) | 描述 |
---|---|
format | 必需。規(guī)定字符串以及如何格式化其中的變量。 可能的格式值:
附加的格式值。必需放置在 % 和字母之間(例如 %.2f):
注釋:如果使用多個(gè)上述的格式值,它們必須按照上面的順序進(jìn)行使用,不能打亂。 |
argarray | 必需。帶有參數(shù)的一個(gè)數(shù)組,這些參數(shù)會(huì)被插到 format 字符串中的 % 符號(hào)處。 |
技術(shù)細(xì)節(jié)
返回值: | 返回被輸出的字符串的長(zhǎng)度。 |
---|---|
PHP 版本: | 4.1.0+ |
更多實(shí)例
實(shí)例 1
使用格式值 %f:
<?php
$num1 = 123;
$num2 = 456;
vprintf("%f%f",array($num1,$num2));
?>
$num1 = 123;
$num2 = 456;
vprintf("%f%f",array($num1,$num2));
?>
運(yùn)行實(shí)例 ?
實(shí)例 2
使用占位符:
<?php
$number = 123;
vprintf("With 2 decimals: %1$.2f
<br>With no decimals: %1$u",array($number));
?>
$number = 123;
vprintf("With 2 decimals: %1$.2f
<br>With no decimals: %1$u",array($number));
?>
運(yùn)行實(shí)例 ?
實(shí)例 3
使用 printf() 來(lái)演示所有可能的格式值:
<?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)
?>
$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)
?>
運(yùn)行實(shí)例 ?
實(shí)例 4
字符串說(shuō)明符的演示:
<?php
$str1 = "Hello";
$str2 = "Hello world!";
vprintf("[%s]<br>",array($str1));
vprintf("[%8s]<br>",array($str1));
vprintf("[%-8s]<br>",array($str1));
vprintf("[%08s]<br>",array($str1));
vprintf("[%'*8s]<br>",array($str1));
vprintf("[%8.8s]<br>",array($str2));
?>
$str1 = "Hello";
$str2 = "Hello world!";
vprintf("[%s]<br>",array($str1));
vprintf("[%8s]<br>",array($str1));
vprintf("[%-8s]<br>",array($str1));
vprintf("[%08s]<br>",array($str1));
vprintf("[%'*8s]<br>",array($str1));
vprintf("[%8.8s]<br>",array($str2));
?>
運(yùn)行實(shí)例 ?

相關(guān)文章
- PHP 常量
- PHP 數(shù)組
- PHP For 循環(huán)
- PHP 命名空間 namespace
- PHP 文件處理
- PHP Session
- PHP 高級(jí)過(guò)濾器
- PHP array_diff() 函數(shù)
- PHP array_fill_keys() 函數(shù)
- PHP array_multisort() 函數(shù)
- PHP array_push() 函數(shù)
- PHP array_walk_recursive() 函數(shù)
- PHP each() 函數(shù)
- PHP key() 函數(shù)
- PHP natcasesort() 函數(shù)
- PHP prev() 函數(shù)
- PHP sizeof() 函數(shù)
- PHP sort() 函數(shù)
- PHP Filter 函數(shù)
- PHP PDO