php return multiple values from function 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: 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 3: php function return multiple values

// Function to swap two numbers 
function swap( $x, $y ) {  
    return array( $y, $x ); 
}

Tags:

Php Example