<?php
// printf 函数用于将格式化的字符串输出。
// 它的第一个参数是格式化字符串,后面的参数则是要插入到格式化字符串中的值。
// 示例 1: 基本用法
$number = 10;
printf("The number is %d", $number);
// 输出: The number is 10
// 示例 2: 使用多个参数
$name = "Alice";
$age = 30;
printf("Name: %s, Age: %d", $name, $age);
// 输出: Name: Alice, Age: 30
// 示例 3: 浮点数格式化
$price = 19.99;
printf("The price is $%.2f", $price);
// 输出: The price is $19.99
// 示例 4: 百分比格式化
$percentage = 75.5;
printf("The percentage is %.1f%%", $percentage);
// 输出: The percentage is 75.5%
// 示例 5: 字符串填充和对齐
$text = "Hello";
printf("[% -10s]", $text);
// 输出: [Hello ]
?>
上一篇:php $_get
下一篇:php 获取字符串最后一个字符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站