简体   繁体   English

php字符串=>浮动铸造不起作用

[英]php string => float casting not working

I have 2 numbers in strings 我的字符串中有2个数字

$xml = "13.52";
$value = "2";

and i want to multiply them. 我想乘以它们

i have tried: 我努力了:

$subtotal = $xml * $value;

Based on the manual, this should work. 根据手册,这应该起作用。 I've already tried 我已经尝试过了

$subtotal = (float)$xml * (float)$value;

and

$xml = floatval($xml);
$value = floatval($value);
$subtotal = $xml * $value;

I have no idea what i have broken. 我不知道我打破了什么。 There is no other help in the manual. 手册中没有其他帮助。

EDIT: looking at the html-source for the variables, it seems there was part of the xml () attached to the variable. 编辑:在html源代码中查找变量,似乎有一部分xml()附加到变量。 Thanks for the help clearing my mind. 多谢您的协助。

Are you sure your problem is where you think it is? 您确定问题出在哪里吗?

Based on the follow code, I get: 根据以下代码,我得到:

<?php
    $xml = "13.52";
    $value = "2";

    $subtotal = $xml * $value;
    echo $subtotal."<br>";

    $subtotal = (float)$xml * (float)$value;
    echo $subtotal."<br>";

    $xml = floatval($xml);
    $value = floatval($value);
    $subtotal = $xml * $value;
    echo $subtotal."<br>";
?>

27.04
27.04
27.04

For the most part, PHP is exceptionally forgiving when it comes to typecasting/juggling. 在大多数情况下,在类型转换/杂耍方面,PHP都非常宽容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM