Returning A String From Functions With PHP

Returning A Variable or String From Functions With PHP

The purpose of this tutorial is to explain how to return a string from a function, which will become part of an array that is created in a double while loop.

<?php
include('connect.inc');
$db=public_db_connect();

$count = 0;
function get_data($var1,$date){
global $db; 


$my_values = $var1.",".$date;
return $my_values; //return a string
}
 
$command = "SELECT DISTINCT id, email FROM table_sort WHERE id IN (1,2,3,4,5,6,7) ORDER BY id ASC";
$result = mysqli_query($db, $command);
 
while($row = mysqli_fetch_assoc($result)) {
$var1 = $row['id'];
 
$command2= "SELECT min(date) as date FROM table_sort WHERE id ='$var1' ORDER BY id ASC";
$result2 = mysqli_query($db, $command2);
 
while($row2 = mysqli_fetch_assoc($result2)){
 
$date = $row2['date'];
echo $var1."-".$date."<br/>";
$get_data = get_data($var1,$date);
$my_array[] = $get_data;
}
}

print_r($my_array);

foreach($my_array as $key => $value) {
echo "<br/>".$key."-".$value;
}

foreach($my_array as $key => $value) {
$all = explode(",",$value);
$id = $all[0];
$date = $all[1];
echo "<br/>".$id."-".$date;
}
 
?>