In a PHP function, sometimes I find that something can be echoed, yet it cannot be returned.
For example, this sucessfully echo’s the result onto the page:
function get_page_id( $path ) {
global $mysqli;
if ($stmt = $mysqli->prepare("SELECT state FROM country WHERE city = ?")) {
$stmt->bind_param("s", $city);
$stmt->execute();
$stmt->bind_result($state);
$stmt->fetch();
$stmt->close();
echo $state;
}
}
However, this function does not return the value:
function get_page_id( $path ) {
global $mysqli;
if ($stmt = $mysqli->prepare("SELECT state FROM country WHERE city = ?")) {
$stmt->bind_param("s", $city);
$stmt->execute();
$stmt->bind_result($state);
$stmt->fetch();
$stmt->close();
return $state;
}
}
What makes a value able to be echoed but not returned?
$state variable is called elsewhere and is included as a global but is not included in the example. What is interesting is that when I change the function to return $state.”; it returns the result as I would expect it to, but only when I append the empty string does it do this. Why could that be?
However, this function does not return the value:I cannot reproduce that behavioure.g.
prints
state: astateon my machine as expected.