Solved

how can my function return more than one value?

Posted on 2016-10-19
12
33 Views
Last Modified: 2016-10-19
Hello guys

I have a function where I pass the id of saler and I need to return some information like: name, phone, email.

A function returns me only one value.

What should I do to have that?

Thanks
Alexandre
0
Comment
Question by:hidrau
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41850066
Make the function return an array with all values you need. Can you post the current code? So we can show you hot to modify to get your goal.
1
 
LVL 1

Author Comment

by:hidrau
ID: 41850077
Hello Marco,

hummm, returning an array, yeah, it can be.

this is my code:

	function saler($idsaler, $cnx){
		$sql = " SELECT NAME, PHONE, EMAIL FROM CADSALER WHERE CADSALER_ID=" .$idsaler;
		$rs= $cnx->query($sql);    	  	
		$row = $rs->fetch_object();	
		return $row->EMAIL;	
	}

Open in new window


I don't know much on PHP and I am learning it according to my small project :)
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 300 total points
ID: 41850093
function saler($idsaler, $cnx){
  $sql = " SELECT NAME, PHONE, EMAIL FROM CADSALER WHERE CADSALER_ID=" .$idsaler;
  $rs= $cnx->query($sql);    	  	

  // ASSUME FAILURE
  $row = false;

  // TEST FOR SUCCESS
  if ($rs) {
     // GET THE ROW
     $row = $rs->fetch_object();	
  }

  // RETURN THE RESULT
  return $row;	
}

Open in new window

Notes on your code
You are using fetch_object - so row is an object and is a  perfect candidate as a return variable
Example using modified saler() function
$return = saler(1, $cnx);
if ($return) {
  // WE GOT A LIVE ONE
  echo "Email: {$return->EMAIL}<br/>";
  echo "Name: {$return->NAME}<br/>";
  echo "Phone: {$return->PHONE}<br/>";
}

Open in new window

1
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 33

Expert Comment

by:ste5an
ID: 41850096
A function returns me only one value.

Correct, cause a function can always only return one value. There is no other way than that.

But you can change the type of that value. You can return one array or one object instead of a single primitive value.
2
 
LVL 1

Author Comment

by:hidrau
ID: 41850130
Julian Hansen, your idea is very good.
I hadn't thought on that.

thanks a lot.

I tried something with array, take a look at it too.

	function saler($idsaler, $cnx){
		$sql = " SELECT NAME, PHONE, EMAIL FROM CADSALER WHERE CADSALER_ID=" .$idsaler;
		$rs= $cnx->query($sql);    	  	
		$row = $rs->fetch_object();	
		return array($row->CADVENDEDOR_ID, $row->NOME, $row->TELEFONE, $row->EMAIL); 
	}

Open in new window


and to get the values, I used the list

    list($vID, $vNome, $vFone, $vEmail) =   saler(1, $cnx);

Open in new window


I haven't run it yet. But is another way that I reach making my research.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 41850141
PHP objects are useful for things like this.  You can return an object that contains all the properties (data elements) and methods (functions) that you like, and these can be addressed directly.  We have an article here at E-E that shows a novel way of looking at the problem.
https://www.experts-exchange.com/articles/18300/Building-a-PHP-Pizza-with-the-Decorator-Pattern.html

You could do something like this:
function saler($idsaler, $cnx){
    $sql = " SELECT NAME, PHONE, EMAIL FROM CADSALER WHERE CADSALER_ID=" .$idsaler;
    $rs= $cnx->query($sql);         
    $row = $rs->fetch_object(); 
    return $row; 
}

Open in new window

You can use var_dump() to visualize the return value from the function.
1
 
LVL 1

Author Comment

by:hidrau
ID: 41850150
Hello Ray Paseur,

Once again, thanks a lot for your post.

I'll study it later on. This is very important to me
1
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41850176
It's important to all of us.  As applications get bigger and more complex, we are all looking for ways to preserve our sanity and avoid lengthy debugging activities.  One of the best ways is to use object-oriented design.  Your data elements can almost always be represented by objects, and these objects can be passed between function calls, being injected into the functions and returned from the functions.
1
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41850228
With reference to your last post

Why do you want to do that? There is no benefit and all you are doing is spinning the CPU's wheels unnecessarily.

An object return is a perfectly acceptable result - why go and create a new array - which is a copy of the object just so you can use list to assign array elements to variables that you could access just as easy through the object return.

BTW (array)$row will convert your object to an array - but I still don't think you should be returning an array and using list - just return the row object and use it in the calling function.
0
 
LVL 1

Author Comment

by:hidrau
ID: 41850689
Julian Hansen,

I only did to learn the possibilities. But I did your example.

Another possibility very good, it is as Ray Paseur told me, but I need to study step by step the OOP.

Thanks
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 41850692
Thanks a lot
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41850702
You are welcome.
1

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question