Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how can my function return more than one value?

Posted on 2016-10-19
12
Medium Priority
?
40 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 59

Accepted Solution

by:
Julian Hansen earned 1200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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 111

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 59

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 59

Expert Comment

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 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