NEWS: Total Picture Views: 147066 | Total Article Views: 155663 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. How to read a JSON and work with it | 3. PickUpThings with Becker Robot | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

Class Example PHP/MySQL

Description:

In this example I will show you basic class. In future article I will show you more advance thing related to class.

A example: https://www.jmdev.ca/sheridan/example/3/

Code:

/********************* Main page **********************/
<?php 
//Include the class intor your PHP script
require("calculation.php");

$calc = new calculation();

//Add 1 + 2 (Echo the result)
echo $calc->add(1,2) . "<br>";

//Sub 2 - 1 (Echo the result)
echo $calc->sub(2,1) . "<br>";

//Mul 2 x 2 (Echo the result)
echo $calc->mul(2,2) . "<br>";

//Div 2 / 2 (Echo the result)
echo $calc->div(2,2) . "<br>"; 
?>

/******************** calculation.php ********************/
<?php

class calculation {
    function add($x,$y){
        if(is_numeric($x) && is_numeric($y)){
            return ($x + $y);
        }else{
            return "Error";
        }
    }

    function sub($x,$y){
        if(is_numeric($x) && is_numeric($y)){
            return ($x - $y);
        }else{
            return "Error";
        }
    }

    function mul($x,$y){
        if(is_numeric($x) && is_numeric($y)){
            return ($x * $y);
        }else{
            return "Error";
        }
    }

    function div($x,$y){
        if($y != 0 && is_numeric($x) && is_numeric($y)){
            return ($x / $y);
        }else{
            return "Error";
        }
    }
} 
?>
Attachments: None

Tags: Class Function

Total Views: 1110

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 12/20/14 and updated on 12/22/14


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.