0

I am trying this code which should perform +, _, x, / but i am getting raw output instead of sum, multiplaction, division, subtraction. here is my code.

if($randf=='1'){$fac='+';}
if($randf=='2'){$fac='-';}
if($randf=='3'){$fac='*';}
if($randf=='4'){$fac='/';}

$ran1=rand(1,100);
$ran2=rand(1,500);
$cans=$ran1.$fac.$ran2;

echo $cans;

but it outputs something like this

10+45

instead of 55. can u please tell where i am doing mistake.

In addition to that, I am trying to perform some filtering function in post. When users submit data in post I am trying to match with some value and then updating to database. Unfortunately, that also fails and always gives 0 as output.

if ($ans2==$ans1)
{
    $marks==1;
}
else {
    $marks=0;
}
mysql_query("INSERT INTO TABLEA () VALUES ()");
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Priya
  • 165
  • 1
  • 12

4 Answers4

3

You're simply treating the numbers and the operators as strings and concatenating them. If you really want to design your code like that, you can use eval(), so your last line would be echo eval($cans);

Otherwise, a better way to do this is to use a switch like @Nouphal.M suggested in his answer.

Note: Keep in mind that using eval() is more expensive than simply doing the arithmetic operation.

Sadiq
  • 2,249
  • 2
  • 24
  • 32
1

I'll not ask why you are doing it.. there are more interesting aproaches like our colleagues pointed.

You can interpret a string that contain the operation as a matematical one like this:

<?
$a = "10+45";
$c = 0;
eval("\$c = $a;");
echo $c; //echoes 55
?>

The eval function will execute the string as a PHP command and $c will have the result.

D. Melo
  • 2,139
  • 2
  • 14
  • 19
  • you rocks boss i am comparing post values before storing to databaseif ($ans2==$ans1) { $marks==1; } else { $marks=0; } mysql_query("INSERT INTO TABLEA () VALUES ()") – Priya Jan 19 '14 at 07:18
  • I'm glad I could help. Take care of that query. ;) – D. Melo Jan 19 '14 at 07:22
0

You're concatenating the numbers and operators as strings not actually evaluating them so the operation never gets performed. The following post might help you.

Variable Operators in PHP

Community
  • 1
  • 1
baarkerlounger
  • 1,217
  • 8
  • 40
  • 57
0

I don't think you can do that. "An operator is something that takes one or more values and yields another value". You cannot consider them as string or either typecast them. Using eval function is not recommended. see the caution here You have to try something like this

$ran1=rand(1,100);
$ran2=rand(1,500);
$cans =0;
switch($randf){
   case "1": 
          $cans= $ran1+$ran2;
   break;
  .....
} 

return $cans;

[Updated part]

The use of mysql_* functions are deprecated, so you should try using mysqli or PDO. Your query is wrong.

The syntax of an insert query is

INSERT INTO your_table_name (field1,field2,...) VALUES (value1,value2,...)

Where your_table_name is your table name, field1,field2.. are the table field names for which you want to save values value1,value2 to be stored respectively.

Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
  • thanks i was doing mistake and right way is to do with switch but in this case where i am doing mistake any idea i am comparing post values before storing to databaseif ($ans2==$ans1) { $marks==1; } else { $marks=0; } mysql_query("INSERT INTO TABLEA () VALUES ()") – Priya Jan 19 '14 at 07:12