<?php
//Autor : R.krishna,
//Website : http://www.sriabiramicomputer.com
//Post your questions or comment, i will try to reply asap.
//Note : This is 100% working example, copy, paste and understand the code in real time.
// This code explains caluclating area of a Rectangle, width * height, sticky form coding, Writing custom function. It's like in-built functions of php.
// The style of this code is Object oriented, It's not procedural. So you should have some knowledge in oops concepts and running php scripts. It is a combination of php, oop, html and css.
// Aim : Getting user input through form text fields and displaying result area of rectangle.
// Check for form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
class Area {
//variable declaration for function without arguments.
var $width=3;
var $height=3;
var $result;
//variable declaration for function with arguments.
var $x;
var $y;
var $result1;
//custom function declaration. Formula area of rectangle = width * //height.
function calculateArea() { // function without arguments.
$result = $this->width*$this->height;
?>
<!-- applying inline css style using div -->
<div class = "red" style = "color:red; font-weight:bold;">
<?php
echo "From custom function without arguments " .$result;
?>
</div>
<?php
echo "<br>";
}// end of function.
//This function gets two inputs through arguments.
function calArea($x,$y ) {
$x = "".$_POST['wid']; // storing value received through //textbox.
$y = "".$_POST['hei'];
$result1 = $x*$y; //Golden part of the logic.
?>
<div class = "orange" style = "color:orange; font-weight:bold;">
<?php
echo "From custom function with argument, width is : " .$x;
echo "<br>";
echo "From custom function with argument, height is " .$y;
echo "<br>";
echo "From custom function with argument, Result Area of rectangle is : " .$result1;
echo "<br>";
echo "<br>";
?>
</div>
<?php
//oop basic knowledge.
// $x = 3; Assign number to variable.
// $tes = $x; Assigining variable to anotother variable.
// echo "With arguments " .$tes;
// echo "<br>";
}// end of function with arguments.
}// end of class area.
// Creating object
$ar = new Area();
//Accessing custom function or method through object.
$ar->calculateArea();
$ar->calArea(4,6); // Here i had passed dummy values 4 and 6, it have no link to actual logic of the program.
// Getting input from textfield and displaying it.
echo 'Width you entered is : ' .$_POST['wid'];
echo "<br>";
echo 'Height you entered is :' .$_POST['hei'];
} // End of server if.
?>
<h1>Area of Traingle - Custom function example through oops way. </h1>
<form action="areaofTriangle.php" method="post">
<p>Enter Width of Rectangle : <input type="text" name="wid" /></p>
<p> Enter Height of Rectangle : <input type = "text" name="hei" /> </p>
<p><input type="submit" name="submit" value="Area of Rectangle" /> </p>
</form>
//Output
//From custom function without arguments 9
//From custom function with argument, width is : 4
//From custom function with argument, height is 2
//From custom function with argument, Result Area of rectangle is : 8
//Width you entered is : 4
//Height you entered is :2
//Area of Traingle - Custom function example through oops way.
//Enter Width of Rectangle : Text box to get width.
//Enter Height of Rectangle : Text box to get height.
//Area of Rectangle : Result Button.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.