Jump to content

Need Help Real Fast [php]


darkside92

Recommended Posts

For Class (high school) i need to make a simple php calcualtor i have it made just cant figure out this error neither can anyone else =/

heres the error Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\php_sandbox\math2.php on line 29

<html>
    <head>
        <title>Untitled Document</title>
    </head>

    <body>
    
    
    <html>
<body>

<form action="$_PHP[SELF]" method="get">
<input type="text" name="num1" />
<select name="sign">
  <option value="*">*</option>
  <option value="-">-</option>
  <option value="+">+</option>
</select>
<input type="text" name="num2" />
<input type="submit" />
</form>

<?php



if($_GET[sign]) {

$val = $_GET[$num1] $_GET[$sign] $_GET[$num2];

echo $val;
}
?>
</body>
</html>
    
    
    </body>
</html>

Link to comment
Share on other sites

$val = $_GET[$num1] $_GET[$sign] $_GET[$num2];

That statement has no logic whatsoever, you'd have to do something like

extract($_GET);
if ($sign == "+")
{
    $val = $num1 + $num2;
} 
elseif ($sign == "-") 
{
    $val = $num1 - $num2;
}

etc...

Link to comment
Share on other sites

<html>
    <head>
        <title>PHP Calculator</title>
    </head>
    <body>
    
   

<form action=<?php echo $_SERVER['PHP_SELF']; ?> method="POST">
<input type="text" name="num1" />
<select name="sign">
  <option value="*">*</option>
    <option value="/">/</option>
  <option value="-">-</option>
  <option value="+">+</option>
</select>
<input type="text" name="num2" />
<input type="submit" />
</form>

<?php
$multi = "*";
$plus = "+";
$min = "-";
$div = "/";

if($_POST['sign'] == $multi) {
$val = ($_POST['num1'] * $_POST['num2']);
echo $_POST['num1'].$_POST['sign'].$_POST['num2']."=".$val;
}

if($_POST['sign'] == $plus) {
$val = ($_POST['num1'] + $_POST['num2']);
echo $_POST['num1'].$_POST['sign'].$_POST['num2']."=".$val;
}

if($_POST['sign'] == $min) {
$val = ($_POST['num1'] - $_POST['num2']);
echo $_POST['num1'].$_POST['sign'].$_POST['num2']."=".$val;
}

if($_POST['sign'] ==     $div) {
$val = ($_POST['num1'] / $_POST['num2']);
echo $_POST['num1'].$_POST['sign'].$_POST['num2']."=".$val;
}



?>
</body>
</html>

I do things the long way, but yeah, it still works. Clean it up and make one function instead of 4.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...