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
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.