dbillings Posted June 16, 2007 Share Posted June 16, 2007 I've setup a form that submits two values and a select menu with a list of bitwise operators. The problem I'm having is I can't get the php to do the math when I display the results it simply displays the values as characters. I'm sure it's something simple that I should be able to figure out on my own. For instance if I submit 0111 >> 2 that is exactly what I see in my php how do I get it to complete the right shift? <?php IF(isset($_REQUEST['submit'])){ $a = $_REQUEST['a']; $b = $_REQUEST['b']; $op = $_REQUEST['operator']; $example= "\$a ". $op ." \$b = "; $bitmath= "$a $op $b"; echo "Format: $example \n"; echo "$a $op $b"; } $operator = array("AND"=>"&","OR"=>"|","XOR"=>"^","NOT"=>"~","SHIFT-LEFT"=>"<<","SHIFT-RIGHT"=>">>"); ?> <fieldset><legend>Bit Wise - Hands on learning</legend> <form name="convert" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <label>$a </label><input type="text" name="a"> <select name="operator"> <?php foreach($operator as $key => $value){ echo "<option title=\"$key\" value=\"$value\">$value</option>"; } ?> </select> <label>$b </label><input type="text" name="b"><br /><br /> <input align="center" type="submit" name="submit" value="Calculate"> </form> </fieldset> Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/ Share on other sites More sharing options...
trq Posted June 16, 2007 Share Posted June 16, 2007 You'll need to use eval. eg; <?php echo "Format: $example \n"; echo eval("$a $op $b"); ?> Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/#findComment-275630 Share on other sites More sharing options...
dbillings Posted June 16, 2007 Author Share Posted June 16, 2007 God bless you. Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/#findComment-275631 Share on other sites More sharing options...
trq Posted June 16, 2007 Share Posted June 16, 2007 No thanks. Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/#findComment-275632 Share on other sites More sharing options...
trq Posted June 16, 2007 Share Posted June 16, 2007 PS: I would be very careful to validate your users input here. It would be VERY easy for someone to inject arbitrary code into your application this way. Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/#findComment-275633 Share on other sites More sharing options...
dbillings Posted June 16, 2007 Author Share Posted June 16, 2007 still gives me $value $value $value with out any calculation. Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/#findComment-275635 Share on other sites More sharing options...
dbillings Posted June 16, 2007 Author Share Posted June 16, 2007 eval appears not to be the answer. Anyone else have a suggestion? Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/#findComment-275639 Share on other sites More sharing options...
dbillings Posted June 16, 2007 Author Share Posted June 16, 2007 Here's a bump with a summary of the problem. I want to turn my post data into an operator and execute the math and display the result for instance.... <?php IF(isset($_REQUEST['submit'])){ $a= $_REQUEST['a']; $b= $_REQUEST['b']; $op= $_REQUEST['operator']; echo "$a $op $b"; } $operator= array("Multiply"=>"*","Divide"=>"/","Add"=>"+","Subtract"=>"-"); ?> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <label>Value $a </label><input type="text" name="a"> <select name="operator"> <option value="">-Operator-</option> <?php foreach($operator as $key => $value){ echo "<option title=\"$key\">$value</option>"; } ?> </select> <label>Value $b </label><input type="text" name="b"> <br /> <input name="submit" type="submit" value="submit"> </form> Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/#findComment-275899 Share on other sites More sharing options...
dbillings Posted June 16, 2007 Author Share Posted June 16, 2007 Good enough. <?php IF(isset($_REQUEST['submit'])){ $a= intval($_REQUEST['a']); $b= intval($_REQUEST['b']); $op= $_REQUEST['operator']; echo "$a $op $b = "; IF($op == "&"){ echo $a & $b; }elseif($op == "|"){ echo $a | $b; }elseif($op == "^"){ echo $a ^ $b; }elseif($op == "~"){ echo ~$a & $b; }elseif($op == ">>"){ echo $a >> $b; }elseif($op == "<<"){ echo $a << $b; } } $default= $_REQUEST['operator']; $operator= array("AND"=>"&","OR"=>"|","XOR"=>"^","NOT"=>"~","Shift left"=>">>","Shift right"=>"<<"); ?> <fieldset><legend>Bitwise examples </legend> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <label>Value $a </label><input type="text" name="a"> <select name="operator"> <option>-Operator-</option> <?php foreach($operator as $key => $value){ IF($default== $value){ $selected= "selected"; }else{ $selected= ""; } echo "<option $selected title=\"$key\">$value</option>"; } ?> </select> <label>Value $b </label><input type="text" name="b"> <br /> <input name="submit" type="submit" value="submit"> </form> </fieldset> Link to comment https://forums.phpfreaks.com/topic/55800-solved-trying-to-perform-calculation-with-post-data/#findComment-275913 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.