Jump to content

[SOLVED] OOP function to return variable


peranha

Recommended Posts

Is there a way to return a variable from a function inside a class?

 

Ex.

 

<?php
//  Function to retrieve user name.
public function retrieve_username($userid) {
	$ruq = mysql_query("SELECT user_name FROM " . $this->pre . "users WHERE user_id = '{$userid}'");
	$r = mysql_fetch_array($ruq) or die(mysql_error());
	$username = $r[0];
	return $username;
}
?>

 

Then I need to use the variable $username in another function.

 

if I echo $username in the function, it will display "peranha", but if I echo $username on the page itself, it doesnt return anything. 

Link to comment
https://forums.phpfreaks.com/topic/147318-solved-oop-function-to-return-variable/
Share on other sites

Here is my usage.

 

<?php
$profile->retrieve_username($userid);
echo $username;
?>

 

if I change the functio to

 

<?php
//  Function to retrieve user name.
   public function retrieve_username($userid) {
      $ruq = mysql_query("SELECT user_name FROM " . $this->pre . "users WHERE user_id = '{$userid}'");
      $r = mysql_fetch_array($ruq) or die(mysql_error());
      $username = $r[0];
      echo $username;
   }
?>

 

"peranha" is outputted to the screen, so $username is set, just wont return with return $username.  Am I not using it right?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.