A simple class in PHP

In this example, a simple class is made in PHP. The class object is created, using which various functions of the class are called

class student
var $foo;
var $bar;	
var $ans;

function student()
$this -> foo = "foo";
$this -> bar = "bar1";	

function display()
  echo $this->foo . " " . $this->bar ."<br>";

function sum($num1, $num2)
  $this->ans = $num1 + $num2;
  return $this->ans;


$std = new student();
$answer = $std->sum(40,50);

echo $answer;



Add a Comment

Your email address will not be published. Required fields are marked *