PHP 字符串 printf()函數(shù)
PHP字符串printf()函數(shù)是預(yù)定義函數(shù)。愛掏網(wǎng) - it200.com它用于輸出格式化字符串。愛掏網(wǎng) - it200.com我們可以將arg1、arg2、arg++參數(shù)傳遞給主字符串中的百分號(%)符號。愛掏網(wǎng) - it200.com
printf(format,arg1,arg2,arg++);
參數(shù) | 描述 | 必填/可選 |
---|---|---|
格式 | 指定字符串。愛掏網(wǎng) - it200.com以下是可能的格式值: |
format:
- %% – 返回百分號
- %b : 二進(jìn)制數(shù)
- %c : 對應(yīng)ASCII值的字符
- %d : 有符號十進(jìn)制數(shù)(負(fù)數(shù)、零或正數(shù))
- %e : 科學(xué)計數(shù)法(小寫)(例如 1.2e+2)
- %E : 科學(xué)計數(shù)法(大寫)(例如 1.2E+2)
- %u : 無符號十進(jìn)制數(shù)(大于或等于零)
- %f : 浮點(diǎn)數(shù)(根據(jù)本地設(shè)置)
- %F : 浮點(diǎn)數(shù)(不考慮本地設(shè)置)
- %g : %e 和 %f 中較短的一個
- %G : %E 和 %f 中較短的一個
- %o : 八進(jìn)制數(shù)
- %s : 字符串
- %x : 十六進(jìn)制數(shù)(小寫字母)
- %X : 十六進(jìn)制數(shù)(大寫字母)
arg1
插入在第一個 %-sign 的參數(shù)。愛掏網(wǎng) - it200.com
arg2
插入在第二個 %-sign 的參數(shù)。愛掏網(wǎng) - it200.com
arg++
插入在第三個、第四個等等 %s sign 的參數(shù)。愛掏網(wǎng) - it200.com
示例1
<?php
version = 7;str = "JAVATPOINT";
printf("We are Learning PHP %u form %s.",version,str);
?>
輸出:
We are Learning PHP 7 form JAVATPOINT.
示例2
<?php
number = 12345;
printf("%f",number);
?>
輸出:
12345.000000
示例3
<?php
number = 23456;
printf("With 2 decimals: %1.2f
<br>With no decimals: %1u",number);
?>
輸出:
With 2 decimals: 23456.00
With no decimals: 23456
示例4
<?php
str1 = "Hello";str2 = "Hello PHP!";
printf("[%s]<br>",str1); // String
printf("[%8s]<br>",str1); // Right-justifies the string with spaces
printf("[%-8s]<br>",str1); // Left-justifies the string value with spaces
printf("[%08s]<br>",str1); // Zero-padding
printf("[%'*8s]<br>",str1); // Adds "*"
printf("[%8.8s]<br>",str2); // Left-justifies the string with spaces (cuts off characters after the specified value)
?>
輸出:
[Hello]
[ Hello]
[Hello ]
[000Hello]
[***Hello]
[Hello PH]
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。