Home >Backend Development >PHP Tutorial >php问题,求指导

php问题,求指导

WBOY
WBOYOriginal
2016-06-06 20:30:551107browse

数据库存的字符串 1/10
获取到字符串之后,如何把1/10 转化为0.1 ?

回复内容:

数据库存的字符串 1/10
获取到字符串之后,如何把1/10 转化为0.1 ?

$a="1/10";
eval("\$a=$a;");
echo $a;

好多种方法,可以explode,然后算

php 有eval函数,可以执行

<code>php</code><code><?php function fractionStrToNumber ($str) {
  $numArr = explode('/', $str);
  if(sizeof($numArr) === 2) {
    return $numArr[0] / $numArr[1];
  } else {
    return 0;
  }
}

echo fractionStrToNumber('1/10');
</code></code>

eval("echo 1/10;");

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn