Solved

how can my function return more than one value?

Posted on 2016-10-19
12
35 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 56

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 110

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 110

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 56

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 56

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

696 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