Two methods to run PHP script

This article explains different methods to write PHP scripts. Basically PHP is a HTML, without using PHP script in it. A PHP script can be inserted between <?php and ?> tags. General contents can be simply displayed using HTML web page to reduce PHP server script runtime duration.

HTML and PHP

HTML: add.html
<html>
<head>
  <title>Addition of Two Numbers</title>
</head>
<body>
  <form action="add.php" method="post">
    Enter the first number: <input type="text" name="a"><br/>
    Enter the second number: <input type="text" name="b"><br/>
  <input type="submit" name="submit" value="ADD">
  </form>
</body>
</html>
PHP: add.php
<html>
<head>
  <title>Addition of Two Numbers</title>
</head>
<body>
  <?php
  $a = $_POST["a"];
  $b = $_POST["b"];
  $c = $a + $b;
  print "Sum of $a and $b is $c.";
  ?>
</body>
</html>

Only using PHP

HTML: index.php
<?php
error_reporting (E_ALL ^ E_NOTICE);
$a = $_POST["a"];
$b = $_POST["b"];
if (!isset($_POST["add"])) {
?>
<html>
<head>
  <title>Addition of Two Numbers</title>
</head>
<body>
  <form method="post" action="<?php echo $PHP_SELF; ?>">
    Enter the first number: <input type="text" name="a"></br/>
    Enter the second number: <input type="text" name="b"></br/>
    <input type="submit" value="ADD" name="add">
  </form>
  <?php
  } else {
    $c = $a + $b;
    print "Sum of $a and $b is $c.";
  }
  ?>
</body>
</html>

Comments

Most Popular Posts

TNEB Bill Calculator

TNEB Bill Calculator (New)

Technical Questions