php 如何在输出和格式数字中回显字符串

php how to echo string in output and format numbers

您好,我正在尝试回显并输出高度正确显示为 5 英尺 8 英寸的位置,但我不知道如何执行此操作。 我是编程新手,所以任何帮助将不胜感激。

最终结果应该是这样的: 以英尺和英寸为单位的高度:5 英尺 8 英寸

$heightMeters = 1.75;
$heightInches = $heightMeters * 100 /2.54;
$heightFeet = $heightInches / 12;
echo 'Height in Feet and inches: '.$heightFeet;

尝试以下操作(代码注释中的解释):

// given height in meters
$heightMeters = 1.75;

// convert the given height into inches
$heightInches = $heightMeters * 100 /2.54;

// feet = integer quotient of inches divided by 12
$heightFeet = floor($heightInches / 12);

// balance inches after $heightfeet
// so if 68 inches, balance would be remainder of 68 divided by 12 = 4
$balanceInches = floor($heightInches % 12);

// prepare display string for the height
$heightStr = 'Height in Feet and inches: ';
// If feet is greater than zero then add it to string
$heightStr .= ($heightFeet > 0 ? $heightFeet . 'ft ' : '');
// If balance inches is greater than zero then add it to string
$heightStr .= ($balanceInches > 0 ? $balanceInches . 'ins' : '');

// Display the string
echo $heightStr;