Embedded php Code printed as written in browser , why?

My php code printed in browser windows as written.
Here is code:

Calculator <?php

class calcu
{

function add($n1,$n2)
{
echo $n1."+".$n2." = ".($n1+$n2);
}

function sub($n1,$n2)
{
echo $n1."-".$n2." = ".($n1-$n2);
}

function mul($n1,$n2)
{
echo $n1.“x”.$n2." = ".($n1*$n2);
}

function div($n1,$n2)
{
echo $n1."/".$n2." = ".($n1/$n2);
}

}

$k=new calcu();
switch($_POST[‘calculate’])
{

case ‘+’:
{
$k->add($_POST[‘val1’],$_POST[‘val2’]);break;
}
case ‘-’:
{
$k->sub($_POST[‘val1’],$_POST[‘val2’]);break;
}
case ‘x’:
{
$k->mul($_POST[‘val1’],$_POST[‘val2’]);break;
}
case ‘/’:
{
$k->div($_POST[‘val1’],$_POST[‘val2’]);break;
}

}
?>

Calculator

1st Value :&nbsp&nbsp
2nd Value :&nbsp&nbsp&nbsp
&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp


Developer:-
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbspSaad Ehsan.

Code is working if i used it as separate php file and link it with html document but requirement is to embed in same document but
Why its being printed insteed of performing tasks ?

Check to make sure that the document has a .php extension, not .htm or .html

Ok.
save as .php
but it shows

" Notice: Undefined index: calculate in D:\01-Softwares\Softwares\xampp\htdocs\test3.php on line 35 "

Results shows but why Notice is shown top of form

The reason for the warning you are receiving is that your code is attempting to run every time the page is loaded, not just when the user has clicked on one of the submit buttons. This means that as soon as the page loads you are attempting to work with a variable that doesn’t yet exist - switch($_POST[‘calculate’]). There are a number of ways to handle this, I am including the way that I would do it below.

Additionally, you are currently allowing the user to attempt to divide by zero, which will also result in a problem. I have also demonstrated a solution for that in the code that follows. It will display an error message to the user, but could be set to do anything you would like.

The last comment is that the html portion of the page is probably OK for testing out your code, but I would not use it in any kind of a production environment. It is obsolete and problematic.

Here is the adjusted code:[php]

Calculator <?php

if(isset($_POST[‘calculate’]))
{
class calcu
{
function add($n1,$n2)
{
echo $n1."+".$n2." = ".($n1+$n2);
}

  function sub($n1,$n2)
    {
      echo $n1."-".$n2." = ".($n1-$n2);
    }

  function mul($n1,$n2)
    {
      echo $n1."x".$n2." = ".($n1*$n2);
    }

  function div($n1,$n2)
    {
      echo $n1."/".$n2." = ".($n1/$n2);
    }
}

$k=new calcu();
switch($_POST[‘calculate’])
{
case ‘+’:{
$k->add($_POST[‘val1’],$_POST[‘val2’]);
break;}
case ‘-’:{
$k->sub($_POST[‘val1’],$_POST[‘val2’]);
break;}
case ‘x’:{
$k->mul($_POST[‘val1’],$_POST[‘val2’]);
break;}
case ‘/’:{
if(!empty($_POST[‘val2’])) $k->div($_POST[‘val1’],$_POST[‘val2’]);
else echo “Error: Division by zero”;
break;}
}
}
?>

Calculator

1st Value :&nbsp&nbsp
2nd Value :&nbsp&nbsp&nbsp
&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp


Developer:-
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbspSaad Ehsan. [/php]

Let me know if it doesn’t make sense or work for you.

Thanks for help
It work and i understood the problem !

Glad to hear it is working for you. Please let me know if you have any other issues.

jay

Sponsor our Newsletter | Privacy Policy | Terms of Service