为什么PHP不打印0值? [英] Why won't PHP print 0 value?

查看:90
本文介绍了为什么PHP不打印0值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在对华氏温度进行摄氏(反之亦然)计算器。所有这些都很好用,但是当我尝试计算32华氏度到摄氏温度时,它应该是0,但什么也不显示。我不明白为什么它不会回显0值。

I have been making a Fahrenheit to Celsius (and vise versa) calculator. All of it works just great, however when I try to calculate 32 fahrenheit to celsius it's supposed to be 0, but instead displays nothing. I do not understand why it will not echo 0 values.

这里是一些代码:

<?php
// Celsius and Fahrenheit Converter
// Programmed by Clyde Cammarata

$error = '<font color="red">Error.</font>';

function tempconvert($temptype, $givenvalue){
    if ($temptype == 'fahrenheit') {
        $celsius = 5/9*($givenvalue-32);
        echo $celsius;
     }

    elseif ($temptype == 'celsius') {
        $fahrenheit = $givenvalue*9/5+32;
        echo $fahrenheit;
    }
    else {
        die($error);
        exit();

    }
}

tempconvert('fahrenheit', '50');

?>


推荐答案

看起来像 $ celcius 的值为0(int类型),而不是 0(字符串类型),因此它不会回显,因为php将其读取为false(0 = false,1 = true)。

looks like $celcius has value 0 (int type) not "0" (string type), so it wont echoed because php read that as false (0 = false, 1 = true).

尝试更改您的代码

echo $celcius;

echo $celcius."";

echo (string) $celcius;

它将变量转换为字符串

这篇关于为什么PHP不打印0值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆