Reputation: 1628
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
Reputation: 5093
You aren't returning your results
public function abc()
{
if ($a==1){
$a++;
}
return $a;
}
Upvotes: 1
Reputation: 1174
I think you forgot to return the value from abc()
public function abc()
{
if ($a=1){
$a+1;
}
return $a;
}
Upvotes: 0
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
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