Php 移动变量时奇怪的浮点到int
下面是我的代码,fetch_data.php返回一个float(汇率)。 第一个echo将返回我需要的浮点值(0.65左右),但第二个echo将返回一个整数(1)。。。为什么?我该如何解决这个问题 我之所以使用include,是因为出于某种原因,PHP返回了file\u get\u内容?我没有很多PHP经验,一个月内没有写一行代码,所以我很生疏Php 移动变量时奇怪的浮点到int,php,Php,下面是我的代码,fetch_data.php返回一个float(汇率)。 第一个echo将返回我需要的浮点值(0.65左右),但第二个echo将返回一个整数(1)。。。为什么?我该如何解决这个问题 我之所以使用include,是因为出于某种原因,PHP返回了file\u get\u内容?我没有很多PHP经验,一个月内没有写一行代码,所以我很生疏 <div class="callout panel"> <p><strong>W
<div class="callout panel">
<p><strong>We sell for:</strong> $<?php
$coin_price = include('fetch_data.php');
$doge = $coin_price;
echo $coin_price;
echo $doge;
?> per 1000</p>
</div>
我们的售价为:每1000美元
获取数据
$string = strip_tags($element); //<strong>$484.66</strong>
$string = str_replace('$', '', $string);
$int = $string;
$val = $int * 1.25;
echo $val / 1000;
$string=strip\u标记($element)//484.66美元
$string=str_replace(“$”,“$string”);
$int=$string;
$val=$int*1.25;
echo$val/1000;
感谢您提供的任何帮助您可以使用
$doge = floatval($coin_price);
检查此处:fetch_data.php
$string = strip_tags($element); //<strong>$484.66</strong>
$string = str_replace('$', '', $string);
$int = $string;
$val = $int * 1.25;
$coin_price = $val / 1000;
$string=strip\u标记($element)//484.66美元
$string=str_replace(“$”,“$string”);
$int=$string;
$val=$int*1.25;
$coin_价格=$val/1000;
其他php文件
<div class="callout panel">
<p><strong>We sell for:</strong>
$<?php
include('fetch_data.php');
echo $coin_price;
$doge = $coin_price;
echo $doge;
?> per 1000</p>
</div>
我们的销售价格为:
每1000美元
如果您想像当前一样使用
include
,我对代码做了一些修改。包含文件时,它包含的代码将继承包含文件所在行的变量范围。从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用。您能提供fetch_data.php的源代码吗?您可能要做的是包含fetch_data.php并从该文件运行一个函数,为$coin_price赋值。您不应该这样使用include。@JoniSalmi为什么file get contents返回php代码?问题可能不是您所怀疑的。也许你的include文件中会有回音。如果您的假设是正确的,您应该得到“每1000美元$0.650.65”。@anu开始使用返回$val/1000代码>而不是echo$val/1000但是,您的总体代码设计非常糟糕。