Jump to content

Is there a way to convert a string to a numeric string ?


Recommended Posts

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"]);
}
?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.