Solved

how can my function return more than one value?

Posted on 2016-10-19
12
34 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 55

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 55

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 55

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP curl issue VERBOSE output 18 80
PHP not parsing ' character 12 46
How to set the Tinymce Editor image path 4 22
Use Select Query to Return Results as a Form 9 23
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

792 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