PHP | vprintf()函数

PHP中的vprintf()函数是一个内置函数,用于将数组值显示为格式化字符串 根据工作类似的格式,将数组值显示为格式化字符串 printf() 但接受一系列参数,而不是参数数量的变量。成功时返回输出字符串的长度。 语法:

null
vprintf (format, array_arguments)

参数:

  1. 格式: 它是一个必需参数,用于指定字符串的格式。 可能的格式值:
    • %% –返回百分号
    • %b –二进制数
    • %d –有符号十进制数(负、零或正)
    • %u –无符号十进制数(等于或大于零)
    • %x –十六进制数(小写字母)
    • %X –十六进制数(大写字母)
    • %f –浮点数(支持本地设置)
    • %F –浮点数(不知道本地设置)
    • %o –八进制数
    • %c –符合ASCII值的字符
    • %s –绳子
    • %e –使用小写字母的科学表示法(例如1.2e+2)
    • %g –短于%e和%f
    • %E –使用大写字母的科学记数法(例如1.2E+2)
    • %G –短于%E和%f
  2. 数组参数 这里的数组参数需要格式化。

项目1: 该程序将显示 %b d u x f o 使用vprintf函数格式化。

php

<?php
$obj = new stdClass();
$obj ->val1 = 9;
$obj ->val2 = 10;
$obj ->val3 = 15;
$obj ->val4 = -1;
echo "using % format: " ;
// below is using of vprintf function
// for printing % format
vprintf( '% % % %' , $obj );
echo "using %b format: " ;
// below is using of vprintf function
// for format %b will print equivalent
// binary number
vprintf( '%b %b %b %b' , $obj );
echo "using %d format: " ;
// below is using of vprintf function
// for  %d format
vprintf( '%d %d %d %d' , $obj );
echo "using %u format: " ;
// below is using of vprintf function
// for  % u (unsigned decimal) format
vprintf( '%u %u %u %u' , $obj );
echo "using %x format: " ;
// below is using of vprintf function
// for  %x  Hexadecimal number (lowercase letters) format
vprintf( '%x %x %x %x' , $obj );
echo "using %X format: " ;
// below is using of vprintf function
// for  %X  Hexadecimal number (uppercase letters) format
vprintf( '%X %X %X %X' , $obj );
echo "using %f format: " ;
// below is using of vprintf function
// for  %f  Floating-point number (local settings aware)
vprintf( '%f %f %f %f' , $obj );
echo "using %F format: " ;
// below is using of vprintf function
// for  %F Floating-point number (not local settings aware)
vprintf( '%F %F %F %F' , $obj );
echo "using %o format: " ;
// below is using of vprintf function
// for  %o octal number
vprintf( '%o %o %o %o' , $obj );
?>


输出:

using % format: % % % %using %b format: 1001 1010 1111 1111111111111111111111111111111111111111111111111111111111111111using %d format: 9 10 15 -1using %u format: 9 10 15 18446744073709551615using %x format: 9 a f ffffffffffffffffusing %X format: 9 A F FFFFFFFFFFFFFFFFusing %f format: 9.000000 10.000000 15.000000 -1.000000using %F format: 9.000000 10.000000 15.000000 -1.000000using %o format: 11 12 17 1777777777777777777777

项目2: 这个节目将展示 c和s 使用vprintf函数格式化。

PHP

<?php
$obj = new stdClass();
$obj ->val1 = 65;
$obj ->val2 = 66;
$obj ->val3 = 97;
$obj ->val4 = 98;
echo "using %c format: " ;
// below is using of vprintf function
// for printing %c format will be print
// ASCII character
vprintf( '%c %c %c %c' , $obj );
echo "using %s format: " ;
// below is using of vprintf function
// for format %s will print as string
vprintf( '%s %s %s %s' , $obj );
?>


输出:

using %c format: A B a busing %s format: 65 66 97 98

方案3: 这个节目将展示 例如 使用vprintf函数格式化。

php

<?php
$obj = new stdClass();
$obj ->val1 = 999999999;
$obj ->val2 = 145956566;
$obj ->val3 = 111111111;
$obj ->val4 = 100000000;
echo "using %e format: " ;
// below is using of vprintf function
// for printing %e format will be print
// Scientific notation (lowercase)
vprintf( '%e %e %e %e' , $obj );
echo "using %g format: " ;
// below is using of vprintf function
// for format %g will be print
// Shorter of %e and %f
vprintf( '%g %g %g %g' , $obj );
echo "using %E format: " ;
// below is using of vprintf function
// for format %E will print
// Scientific notation (uppercase)
vprintf( '%E %E %E %E' , $obj );
echo "using %G format: " ;
// below is using of vprintf function
// for format %G will be print
// Shorter of %E and %f
vprintf( '%G %G %G %G' , $obj );
?>


输出:

using %e format: 1.000000e+9 1.459566e+8 1.111111e+8 1.000000e+8using %g format: 1.0e+9 1.45957e+8 1.11111e+8 1.0e+8using %E format: 1.000000E+9 1.459566E+8 1.111111E+8 1.000000E+8using %G format: 1.0E+9 1.45957E+8 1.11111E+8 1.0E+8

方案4: 在这个程序中,所有四个变量将分别用vprintf函数打印10 20 30 40个空格。

PHP

<?php
$obj = new stdClass();
$obj ->val1 = 'gfg 1' ;
$obj ->val2 = 'gfg 2' ;
$obj ->val3 = 'gfg 3' ;
$obj ->val4 = 'gfg 4' ;
// below is using of vprintf function
vprintf( '%-10s %-20s %-30s %-40s' , $obj );
?>


输出:

gfg 1      gfg 2                gfg 3                          gfg 4

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享