php return multiple values code example
Example 1: return two variables php
function getXYZ()
{
return array(4,5,6);
}
list($x,$y,$z) = getXYZ();
// Afterwards: $x == 4 && $y == 5 && $z == 6
// (This will hold for all samples unless otherwise noted)
Example 2: in array php multiple values
$haystack = array(...);
$target = array('foo', 'bar');
if(count(array_intersect($haystack, $target)) == count($target)){
// all of $target is in $haystack
}
if(count(array_intersect($haystack, $target)) > 0){
// at least one of $target is in $haystack
}
Example 3: php return array
<?php
function data() {
$out[0] = "abc";
$out[1] = "def";
$out[2] = "ghi";
return $out;
}
$data = data();
foreach($data as $items){
echo $items;
}
Example 4: php function return multiple values
// Function to swap two numbers
function swap( $x, $y ) {
return array( $y, $x );
}