Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments before begin php tag returns json with garbage #569

Closed
neo-angeiras opened this issue Apr 8, 2024 · 4 comments
Closed

Comments before begin php tag returns json with garbage #569

neo-angeiras opened this issue Apr 8, 2024 · 4 comments

Comments

@neo-angeiras
Copy link

When I add a comment before php begin tag (<?php), the json returned by Flight::json(...) includes the comment. For instance:

//
// Comment before php tag
//
<?php
$usr_array = array("status" => true, "id_product" => 123456, "name" => "test test test");
$ret_code = 200;
Flight::json($user_arr, $ret_code);
?>

should return only :

{
   "status": true,
   "id_product": "123456",
   "name": "test test test"
} 

but returns:

{
   "status": true,
   "id_product": "123456",
   "name": "test test test"
} 
//
// Comment before php tag
//

If the comment goes after <?php, it returns without garbage
In flight 2.0.2, with same php version (7.4), works fine

@n0nag0n
Copy link
Collaborator

n0nag0n commented Apr 8, 2024

So the reason why it's probably complain is because when you add // comments outside of PHP, it treats that like regular ol' plain text. The reason why it works in 2.0.2 is because it used to wipe out any output buffer before it even got started. You actually could probably have it still work <3.5.0 if you wanted. You also could use Flight::set('flight.v2.output_buffering', true); to make it work like it used to.

My recommendation going forward would be to put your comments actually inside the <?php tags so they are treated as such. Having "comments" outside of PHP code is not an intended use case.

@BelleNottelling
Copy link
Contributor

BelleNottelling commented Apr 8, 2024

To further add: it's not that comments written before <?php are treated like regular text it's that PHP doesn't even parse anything outside of a PHP tag it's and is simply kept as-is and added to the content of the page.

This is by design and a great example of why is actually on the PHP wiki:

<!DOCTYPE html>
<html>
    <head>
        <title>PHP Test</title>
    </head>
    <body>
        <?php echo '<p>Hello World</p>'; ?>
    </body>
</html>

@neo-angeiras
Copy link
Author

I already adjust all my PHP files, just a few for my first Flight API (and, in fact, my first ever PHP job)

Thanks for the response.

@n0nag0n
Copy link
Collaborator

n0nag0n commented Apr 8, 2024

Congrats on your first job! I remember my first job and how exciting it was. Please keep in touch to see how we can help you be as successful as possible!

@n0nag0n n0nag0n closed this as completed Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants