Jump to content

[SOLVED] php functions


adam291086

Recommended Posts

i have this php function

 

function edit($id)
{

	require_once('DbConnector.php');
	$loginConnector = new DbConnector();
	$getnote = $loginConnector->query("Select * FROM Notepad WHERE Id ='$id'");
	while ($this->notedata = $loginConnector->fetchArray($getnote))
		{	
		$text = $this->notedata['text'];
		$id = $this->notedata['Id'];
		echo $text;
		}
}

 

now it gets the information from my database. But instead of echoing out the information i want to return the values of $text to be used else where within the iniation script.

 

This is how i call upon the function

 

$edit = new note();
$id = $_GET['id'];
$edit->edit($id);

 

 

Link to comment
https://forums.phpfreaks.com/topic/124483-solved-php-functions/
Share on other sites

function edit($id)
{
     require_once('DbConnector.php');
     $loginConnector = new DbConnector();
     $getnote = $loginConnector->query("Select * FROM Notepad WHERE Id ='$id'");
     while ($this->notedata = $loginConnector->fetchArray($getnote))
     {	
  $text = $this->notedata['text'];
  $id = $this->notedata['Id'];
  echo $text;
      }
      
      return $text;
}

 

$edit = new note();
$id = $_GET['id'];
$edit_text = $edit->edit($id);

 

Of course you may want to do some checking before you return $text, i.e., to see if it's a value.

Link to comment
https://forums.phpfreaks.com/topic/124483-solved-php-functions/#findComment-642832
Share on other sites

Well because you're echoing out the $text variable you can't just return the value at the end. You could try saving the outputs into one string, or an array, and then return that?

 

To return a value simply add "return $varname;" to the end, or wherever in the function. The function will stop when it reaches return...

 

Then you'd use something like:

 

$text = edit($id);

 

Adam

Link to comment
https://forums.phpfreaks.com/topic/124483-solved-php-functions/#findComment-642839
Share on other sites

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.