Irresistable Posted January 3, 2010 Share Posted January 3, 2010 I have some variables set, and I'm trying to find the answer of them... but it doesn't work. $pri = 1 $met = + $sec = 1 $answer = $pri . $met . $sec; The sum techncially is 1+1 Why isn't $answer 2? Link to comment https://forums.phpfreaks.com/topic/187044-how-to-get-the-answer-to-this-sum/ Share on other sites More sharing options...
premiso Posted January 3, 2010 Share Posted January 3, 2010 Because you are just concatenating a string. As far as I know the only way to do what you want is something like this: $pri = 1; $met = '+'; $sec = 1; switch ($met) { case '+': $answer = $pri + $sec; break; case '-': $answer = $pri - $sec; break; } You may be able to use eval but I would suggest against it. Here is how that would be done: eval('$answer = $pri ' . $met . ' $sec'); echo $answer; If you go that route make sure to filter your data, as eval can be dangerous if the data going into it is not checked / validated / filtered. Link to comment https://forums.phpfreaks.com/topic/187044-how-to-get-the-answer-to-this-sum/#findComment-987764 Share on other sites More sharing options...
Irresistable Posted January 3, 2010 Author Share Posted January 3, 2010 Parse error: syntax error, unexpected '=' in /home/jeanie/public_html/recaptcha/pngimg.php on line 51 sum = "$pri + $met ="; How do I solve it ? Link to comment https://forums.phpfreaks.com/topic/187044-how-to-get-the-answer-to-this-sum/#findComment-987788 Share on other sites More sharing options...
ignace Posted January 3, 2010 Share Posted January 3, 2010 Don't double post you already have a thread about this http://www.phpfreaks.com/forums/index.php/topic,282626.msg1339975.html#msg1339975 Link to comment https://forums.phpfreaks.com/topic/187044-how-to-get-the-answer-to-this-sum/#findComment-987795 Share on other sites More sharing options...
premiso Posted January 3, 2010 Share Posted January 3, 2010 Use correct syntax. $sum = "$pri + $met ="; Link to comment https://forums.phpfreaks.com/topic/187044-how-to-get-the-answer-to-this-sum/#findComment-987798 Share on other sites More sharing options...
Irresistable Posted January 3, 2010 Author Share Posted January 3, 2010 Hmm.. I'm sorry. I didn't quite see it as a double post. Yeah sorry. That was a common typo I didn't notice. Link to comment https://forums.phpfreaks.com/topic/187044-how-to-get-the-answer-to-this-sum/#findComment-987799 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.