Solved

how can my function return more than one value?

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

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 51

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
 
LVL 32

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 108

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 108

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 51

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 51

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now