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

时间:2018-10-14 18:04:48

标签: php string output echo

嗨,我正在尝试回显和输出高度正确显示为5ft 8ins的位置,但是我不知道如何执行此操作。 我是编程新手,所以将不胜感激。

最终结果应类似于: 以英尺和英寸为单位的高度:5英尺8英寸

Poster.propTypes = {
  responsive: PropTypes.string
}

1 个答案:

答案 0 :(得分:1)

尝试以下操作(在代码注释中进行解释):

// 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;