0

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;  
    ?>
1
  • 1
    No it must not. But you should get a lot of warnings and notices and even errors. This needs basic debugging first, enable error reporting to the highest level possible, log errors and track the error log. Refer to the PHP reference of your choice to learn about error reporting and logging. Commented Jul 12, 2012 at 17:47

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

might want to add something about returning the value for $a
2

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

return $a;

You'll get something in the echo.

Comments

1

You aren't returning your results

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

   return $a;
 }  

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.