1

My API End point has return statement instead of print or echo : e.g.

return json_encode($data);
instead of
print json_encode($data);

I have a curl caller to consume my API. When I try to display the result, it shows null. However if I change API End point to :

print json_encode($data);

I can see the result.

Any idea how can I deal with return statement.

5
  • Why? If print (or echo) does what you want, why do you want to use return? Jun 18, 2018 at 11:55
  • I am eager to know how do I deal with return. Jun 18, 2018 at 11:56
  • 1
    You don't. If you want output you need to use print or echo. The whole point of return is to not do that. return is for passing data between functions/variables. Jun 18, 2018 at 11:58
  • Wait, wait... Are you talking about REST API endpoints? If so, then it should return it's value and not print it, I believe... Could you show more of your code? Jun 18, 2018 at 16:40
  • Something like below: Inside routes.php: Router::connect('/api/fetch', array( 'Controller' => 'FetchController', Action' => 'fetch', 'Method' => 'GET' ), $_GET ); Inside Controller: public function fetch( $DATA ) { echo "100"; //works #return 100; //doesn't } Jun 19, 2018 at 5:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.