Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9012529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:01:23+00:00 2026-06-16T03:01:23+00:00

In a PHP function, sometimes I find that something can be echoed, yet it

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:01:24+00:00Added an answer on June 16, 2026 at 3:01 am

    However, this function does not return the value: I cannot reproduce that behaviour

    e.g.

    <?php
    $mysqli = new mysqli('localhost', 'localonly', 'localonly', 'test');
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
    }
    setup($mysqli);
    
    $state = get_page_id('acity');
    echo 'state: ', $state;
    
    function get_page_id( $city ) {
        global $mysqli;
        if ($stmt = $mysqli->prepare("SELECT state FROM tmp_country WHERE city = ?")) {
            $stmt->bind_param("s", $city);
            $stmt->execute();
            $stmt->bind_result($state);
            $stmt->fetch();
            $stmt->close();
            return $state;
        }
    }
    
    
    function setup($mysqli) {
        $mysqli->query('
            CREATE TEMPORARY TABLE tmp_country (
                id int auto_increment,
                city varchar(32),
                state varchar(32),
                primary key(id),
                unique key(city)
            )
        ') or die($mysqli->error);
        $mysqli->query("INSERT INTO tmp_country (city,state) VALUES ('acity','astate'),('bcity','bstate')") or die($mysqli->error);
    }
    

    prints state: astate on my machine as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a PHP function which fetches a Curl request. This request sometimes take longer
I sometimes use user data in php function header like this : header('Location :
I have a basic PHP function that I am working with. Sometimes, it is
Sometimes when I can't find the way of deactivating a function or any other
I have a PHP function that extract dat of an invoice from DB. The
I created a PHP function that will automatically turn URLs into links, link Twitter
I have a PHP function that builds a JSON array via $jsonArray= array(); for
I want to have a generic PHP function that builds a mysql query based
I have php scripts that call perl scripts to do various things and sometimes
I work with PHP and Javascript a lot and I find that I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.