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.

  • Why? If print (or echo) does what you want, why do you want to use return? – Jacob Peattie Jun 18 '18 at 11:55
  • I am eager to know how do I deal with return. – Shrinath Shetty Jun 18 '18 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. – Jacob Peattie Jun 18 '18 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? – Krzysiek Dróżdż Jun 18 '18 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 } – Shrinath Shetty Jun 19 '18 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.