Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http_build_query turns not_var=yes into ¬_var=yes in some PHP configurations. Why?

Tags:

php

This code:

$query = array(
    "var" => "no",
    "not_var" => "yes",
    "var2" => "maybe"
);
print http_build_query($query);

Outputs:

var=no¬_var=yes&var2=maybe

This happens on my own machine running PHP 5.3.19. I've reproduced this behavior on PHPfiddle. It works as expected on ideone.com running PHP 5.2.11.

Why does this happen?

like image 823
HertzaHaeon Avatar asked Dec 26 '22 11:12

HertzaHaeon


2 Answers

This is only because of your browser encoding the &not entity, try this:

print htmlentities(http_build_query($query));

For normal usage, it will be absolutely fine.

The reason it's different on ideone vs PHPFiddle is because PHPFiddle just dumps the results into an iframe, and ideone is displaying it pre-entity encoded so that other displays aren't broken.

like image 171
Rudi Visser Avatar answered May 08 '23 08:05

Rudi Visser


Have you done a "view source" on the result, or are you relying on the browser to show it?

In fact, it is outputting the string as expected; it is the browser that is interpreting it incorrectly.

The string contains &not. This is being interpreted by the browser as an HTML entity, despite the fact that it is missing the trailing semicolon.

If you view source, you'll see that the output is actually correct.

Solution: If you want this string to be included in an HTML page, you should htmlencode it as well.

like image 44
SDC Avatar answered May 08 '23 07:05

SDC