Jump to content

Recommended Posts

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>

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>

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>

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.