473,322 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Make a function that returns more than one value?

Is there any ways to make a function like system(), which returns more
than one value (true or false) and (the array)

Sep 25 '06 #1
7 2373
What about reference-passed variable as an argument?

For example:

<?php

function MyFunc ($input, &$return_array)
{
if (!empty($input))
{
$return_array = Array($input, date('H:m:s'));
return true;
}
else
return false;
}

$a = Array();
if (MyFunc('Hello', $a))
print_r($a);
else
echo 'Empty input!';
?>

For details on arguments passed by reference:
http://cz.php.net/manual/en/functions.arguments.php

The87Boy napsal(a):
Is there any ways to make a function like system(), which returns more
than one value (true or false) and (the array)
Sep 25 '06 #2
The87Boy said the following on 25/09/2006 16:44:
Is there any ways to make a function like system(), which returns more
than one value (true or false) and (the array)
A function can have only one return value. However, it can alter
variables passed in by reference, which is what happens with system().

See http://uk.php.net/manual/en/language.references.php.

--
Oli
Sep 25 '06 #3
Oli Filth wrote:
The87Boy said the following on 25/09/2006 16:44:
>Is there any ways to make a function like system(), which returns more
than one value (true or false) and (the array)

A function can have only one return value. However, it can alter
variables passed in by reference, which is what happens with system().

See http://uk.php.net/manual/en/language.references.php.
Another option is to use an array to return the values you want to be
returned, of course this will not be in the same manner as system() does, but
can sometimes be a smother way to return multiple values.
//Aho
Sep 25 '06 #4

"The87Boy" <th******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Is there any ways to make a function like system(), which returns more
than one value (true or false) and (the array)
It is something I've done many times in c++ but not so much in PHP...
that one thing returned could be an object which has whatever you want in
it.
here is the relevant line from the docs
http://us2.php.net/manual/en/functio...ing-values.php
" Values are returned by using the optional return statement. Any type may
be returned, including lists and objects."

here is a quick example:
<?php
class stuff { # a container for all your stuff
var $the_bool;
var $the_array;
function stuff($b,$a) {
$this->the_bool = $b;
$this->the_array = $a;
}
function get_bool () {
return $this->the_bool;
}
function get_array () {
return $this->the_array;
}
}
$a[] = "bread";
$a[] = "butter";
$a[] = "salt";
var_dump($a);
echo "<br/>var dump thing :<br />";
$thing = new stuff(true,$a);
var_dump($thing);
echo "<br />the bool is ".$thing->get_bool();
$a1 = $thing->get_array();
echo "<br/>var dump a1 is<br />";
var_dump($a1);
echo "<br />the array 0 is ".$a1[0];
echo "<br />the array 1 is ".$a1[1];
echo "<br />the array 2 is ".$a1[2];

$arr[] = "49";
$arr[] = "sugar";
$arr[] = "37.294";

function demo($bl,$ar) {
$c = new stuff($bl,$ar);
return $c;
}
$d = demo(true,$arr);
echo "<br/><br/>d->bool returned from demo is ".$d->get_bool();
$da = $d->get_array();
echo "<br/>d->a[0] returned from demo is ".$da[0];
echo "<br/>d->a[1] returned from demo is ".$da[1];
echo "<br/>d->a[2] returned from demo is ".$da[2];

?>
Sep 25 '06 #5
It was just what I was searching for, but I didn't now, it existed

Jiri Fogl wrote:
What about reference-passed variable as an argument?

For example:

[Some code...]

For details on arguments passed by reference:
http://php.net/manual/en/functions.arguments.php
Sep 25 '06 #6
The87Boy:

Why not just returning an array (of arrays (of arrays (...))) ...?
It was just what I was searching for, but I didn't now, it existed

Jiri Fogl wrote:
>What about reference-passed variable as an argument?

For example:

[Some code...]

For details on arguments passed by reference:
http://php.net/manual/en/functions.arguments.php
Sep 25 '06 #7
Jiri Fogl schrieb:
What about reference-passed variable as an argument?

For example:

<?php

function MyFunc ($input, &$return_array)
{
if (!empty($input))
{
$return_array = Array($input, date('H:m:s'));
return true;
}
else
return false;
}

$a = Array();
if (MyFunc('Hello', $a))
print_r($a);
else
echo 'Empty input!';
?>
Just as an addition - this special case could also be handled without a
reference, for example:

<?php
function MyFunc ($input)
{
if (!empty($input))
return array($input, date('H:m:s'));
else
return false;
}

if ($a = MyFunc('Hello'))
print_r($a);
else
echo 'Empty input!';
?>

Or, if you need to change an existing array:

<?php
function MyFunc ($input, $arr)
{
if (!empty($input)) {
$arr[] = $input;
$arr[] = date('H:m:s');
return $arr;
}
else
return false;
}

$arr = array('some', 'data');
if ($new_arr = MyFunc('Hello', $arr))
$arr = $new_arr;
else
echo 'Empty input!';
print_r($arr);
?>

--
Markus
Sep 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Michael | last post by:
Guys, I'm interested in how the compiler implements function calls, can anyone correct my understanding/point me towards some good articles. When a function is called, is the stack pointer...
2
by: Steven Burn | last post by:
..:: The Specs: MS Access 2000 (host charges extra for SQL/MySQL) MS Windows Server 2003 (prod) / MS XP SP1 (dev) ..:: The setup: The database has been setup with two tables; tblDownloads
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
2
by: Sam | last post by:
In our C++ program, we are using the system call to execute another C++ program synchronously. The program executed by system runs without error and returns back a 0. Under conditions we cannot...
19
by: centurian | last post by:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++ 6" and it ran without any errors. Does this thing make any sense? Is it of some use or some problem with grammar/CFG of...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.