Hi,

I was writing the code for the class to test passing of values. Here's my code.

<?php
class demo{

    public function setMynum($a,$b){
       $varA=$a;
        $varB=$b;
        return $varA;
        return $varB;
                   }
          
}
$obj=new demo;
echo $obj->setMynum(12,13);
?>

Output is 12.
What is wrong with the code? I guess i have done everything to set things correct but something is missing i guess, any idea?

return stops execution of your function, and returns the specified value to the caller, thus outputting $a (which is 12). Line 8 is never executed.

What are you trying to do?

Thanks I wanted to display the result. I got confused in between using return or simply echo. I think i need to understand more about return statement usage and where to and not to avoid them.

Hi,

I found echoing $a and $b kind of boring, so I have to make something up to make it a little bit interesting. Here is the basic setup of what you are trying to achieved..not sure, but that's my only guess.

you cannot get a result by doing return $this; and return $thisAlso; .. Due to the script is already been stopped on the very first return $this..

here is a simple script where you play around with values ... I just made this up and I don't have the time to test it locally, but I am sure it should echo something if not error :)..when you do get an errors, please let us know..

<?php
class demo{
var $a ; ## where r u?
var $b ; ## how about me?
var $c;

    function demo(){
	#the constructor needed for the function lets_echoAll below, but NOT necessary for the first two functions below
	## this is intended to be empty and MUST be empty. NOtice it has the same name as the class "demo"?
	}
    public function multi_Mynum($val1,$val2,$val3){
       $this->a = ( $val1 * $val2 * $val3 );
	 return $this->a; ## a 'm here..
	 }
	 function q_Mynum($val1,$val2,$val3){
	 	 if($val1 <=0) $val1 = 1; ## we don't want zero here
	 $this->b = ($val2 + $val3) / ($val1); ## $val1 CANNOT be equal to ZERO
	
	   return $this->b; ## you should b here
	 
         }
    
     function lets_echoAll(){
	 ## the reason we are able to echo these @ all, because of the constructor above.
	     $this->c = '<br/>This is for the var $a and $this->a:  '.$this->a.'<br/> This is for the var $b and $this-> b:  '.$this->b.'<br/>';
		 ## lets use echo this time around..
		 echo $this->c;

}	 
}
$obj = new demo;
echo $obj->multi_Mynum(5,10,20)."<br/>";
echo $obj->q_Mynum(5,10,20);
## obj below has been echoed in the function, so we don't hav to here.
$obj->lets_echoAll();

?>

You can also predefined these $val1,$val2,$val3 just right after the var $c ... somthing like this var $val1 = "somethingHere";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.