//sample Pseudo Code
function djikstra($source, $destination){
//after some computations....
return $distance_taken, $path;
}
$distance_taken
is an integer and $path
is an array.
//sample Pseudo Code
function djikstra($source, $destination){
//after some computations....
return $distance_taken, $path;
}
$distance_taken
is an integer and $path
is an array.
You can't return more than one value. But you can return an array:
function djikstra($source, $destination){
//after some computations....
return [$distance_taken, $path];
}
So when you call this function, you can make use of list
to get the values:
list($distance_taken, $path) = djikstra($source, $destination);
You can just return one or no value
return array('distance_token' => $distance_taken, 'path' => $path);
Then access them by
$result = djikstra($source, $destination);
$distance_taken = $result['distance_token'];
$path = $result['path'];
A function always return one type. You can use an array if your variables are the same type.
But The best way to do that is to create an Object
<?php
class DikjstraObj {
var $source;
var $destination;
function __construct($source, $destination) {
$this->source = $source;
$this->destination = $destination;
}
}
// ///////////////
// in your function simply do
function djikstra($source, $destination){
//after some computations....
$dObj = new DikjstraObj($source, $destination);
return $dObj ;
}
Use Array
return array('distance_taken' => $distance_taken, 'path' => $path);
Use JSON
return json_encode(array('distance_taken' => $distance_taken, 'path' => $path));
You can't basically but you can create a class. PHP documentation: http://php.net/manual/en/language.oop5.php
Ok, as we can't go without reference, I put an answer too, and summarize the others goods ones:
C
pointer like)Reference can be used when you want to directly work with the variable without making a temp copy and returning a new value to erase the old one. This is the way C language work, and can improve performance for big variable manipulation.
To use reference, you just have to put a &
before the $
of your function parameters:
<?
function djikstra(&$source, &$destination){
//after some computations....
$source = 'new val 1';
$destination = 'new val 2';
// return can be omited or used to handle & return error
return ;
}
// init
$source = 'default val 1';
$destination = 'default val 2';
// will change the val on the fly
djikstra($source, $destination);
// will print "new val 1 - new val 2"
echo $source. ' - ' .$destination;
Other goods ways :
As said by @Thamilan, this is probably the most common way.
You can't return more than one value. But you can return an array:
function djikstra($source, $destination){ //after some computations.... return [$distance_taken, $path]; // or `return array($distance_taken, $path)` }
So when you call this function, you can make use of
list
to get the values:list($distance_taken, $path) = djikstra($source, $destination);
Nb: careful with list()
, it's a tricky function to use, read the doc
The @Topsy is a good way too if you already code in Oriented Object style, as you'll have to apply function to full object and expect the full modified object back.
Avoid this and go for #1 or #2 if you work in procedural.
A function always return one type. You can use an array if your variables are the same type.
But The best way to do that is to create an Object
<?php class DikjstraObj { var $source; var $destination; function __construct($source, $destination) { $this->source = $source; $this->destination = $destination; } } // /////////////// // in your function simply do function djikstra($source, $destination){ //after some computations.... $dObj = new DikjstraObj($source, $destination); return $dObj ; }