jd2007 Posted August 11, 2007 Share Posted August 11, 2007 Is there a way to convert a string to a numeric string ? Link to comment https://forums.phpfreaks.com/topic/64394-is-there-a-way-to-convert-a-string-to-a-numeric-string/ Share on other sites More sharing options...
Guest Posted August 11, 2007 Share Posted August 11, 2007 You can try casting. I'm not entirely sure what you mean though: <?php $int = 23; $string = (string)$int; // OR $string = "$int"; ?> Whichever you prefer Link to comment https://forums.phpfreaks.com/topic/64394-is-there-a-way-to-convert-a-string-to-a-numeric-string/#findComment-321059 Share on other sites More sharing options...
jd2007 Posted August 11, 2007 Author Share Posted August 11, 2007 i want this string '9-1' to give me the answer 8. Link to comment https://forums.phpfreaks.com/topic/64394-is-there-a-way-to-convert-a-string-to-a-numeric-string/#findComment-321065 Share on other sites More sharing options...
AndyB Posted August 11, 2007 Share Posted August 11, 2007 eval() Link to comment https://forums.phpfreaks.com/topic/64394-is-there-a-way-to-convert-a-string-to-a-numeric-string/#findComment-321066 Share on other sites More sharing options...
jd2007 Posted August 11, 2007 Author Share Posted August 11, 2007 i get this : Parse error: syntax error, unexpected T_LNUMBER in C:\AppServ\www\Calculator\calc.php(34) : eval()'d code on line 1 my code: <?php class Calculator { private $localhost; private $username; private $password; private $db; function connectdb($l, $u, $p, $d) { $this->localhost=$l; $this->username=$u; $this->password=$p; $this->db=$d; mysql_connect($this->localhost, $this->username, $this->password); mysql_select_db($this->db); } function checkvalue($value) { $this->que="select math from calcs where cal='$value'"; $this->result=mysql_query($this->que); $this->row=mysql_fetch_row($this->result); if ($this->row[0]=="add") { echo "+"; } echo $this->row[0]; } function result($exp) { echo eval($exp); } } //echo (3+(3/4))-(5/6)+(1+(1/3)); $cal=new Calculator; $cal->connectdb("localhost", "root", "*****", "calculator"); $cal->checkvalue($_GET["n"]); if ($_GET["n2"]=="") { } else { $cal->result($_GET["n2"]); } ?> Link to comment https://forums.phpfreaks.com/topic/64394-is-there-a-way-to-convert-a-string-to-a-numeric-string/#findComment-321071 Share on other sites More sharing options...
Guest Posted August 11, 2007 Share Posted August 11, 2007 I may be wrong, but I believe eval() can't be used with variables (it has to be raw string data). EDIT: You can, nevermind. However, you have to put valid PHP inside that eval... You will need that trailing semicolon at the end of the statement inside eval(), and simply putting in the expression into eval will get you nothing. You need to add an echo or something <?php $str = '1+2'; $s = '$x = '.$str.';'; eval($s); ?> Theoretically, that should work. But hey, 9:30am and PHP don't mix XD Link to comment https://forums.phpfreaks.com/topic/64394-is-there-a-way-to-convert-a-string-to-a-numeric-string/#findComment-321075 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.