rahul dagli
rahul dagli

Reputation: 1628

Simple php program displaying no output or error msg

This is my first program in oop php. Its very simple where i would like to add a numerical value to a variable. And program must output 2.

 <?php
    class MyClass  
    {  
        public $a = 1;  

        public function abc()  
        {  
            if ($a=1){
                $a+1;   
            }  
        }   
    }  

    $obj = new MyClass;  

    echo $obj->abc;  
    ?>

Upvotes: 0

Views: 446

Answers (4)

JoeCortopassi
JoeCortopassi

Reputation: 5093

You aren't returning your results

public function abc()  
{  
   if ($a==1){
      $a++;   
   }  

   return $a;
 }  

Upvotes: 1

Yash Dayal
Yash Dayal

Reputation: 1174

I think you forgot to return the value from abc()

 public function abc()  
        {  
            if ($a=1){
                $a+1;   
            }  
            return $a;
        }   

Upvotes: 0

andrewsi
andrewsi

Reputation: 10732

In addition to gview's answer:

if ($a=1){
    $a+1;   
}  

Should be:

if ($a == 1){
    $a = $a + 1;   
}  

The = operator is for assignment, not for comparisons.

Upvotes: 3

gview
gview

Reputation: 15371

The abc() function does not return anything. Thus you get no output. If you add:

return $a;

You'll get something in the echo.

Upvotes: 2

Related Questions