Beginning php. Variable does not work how it should?


i know a little php but now i want to learn more. So i start by making a little static website a little dynamic that its easier to maintain. So that i don’t need to change 10 html pages if i want to change something in the header.

So i’m trying to set the variable for the xu-a-compatible mode.
This is a variable?:

$xua = header('X-UA-Compatible: IE=edge,chrome=1');

and i set it above the html doctype.

Than i want to print it in the html head:

<?php print $xua; ?>

But the funny thing is that IE react’s even if i don’t put the print function in the head. Why is that? I mean the first line is just a variable and should not be seen by the browser???

Oh wait. The header is something the server sends. So there is no need to prind it in the html head. I was thinking the metatag way. So:

<?php header('X-UA-Compatible: IE=edge,chrome=1'); ?>

above the doctype is enough - right?

But i still wonder why is it send if i set it as a variable?

header() function returns nothing to set to a variable.

Below is the function declaration from the PHP manual, the first word is the return type. Void means it returns nothing.
void [B]header[/B] ( string $string [, bool $replace = true [, int $http_response_code ]] )

Now one thing to know, you do not need this header if your code is standards complaints. You only really need it if its not.

you need it to turn off the compatible mode switch, to make sure IE always uses standards mode and to tell IE to use Chrome frame if it is installed.

PHP’s [fphp]header[/fphp] function has absolutely nothing to do with the HTML <head> section.

I don’t know why you need a variable at all, if you want this meta tag in your <head> section. Just put it directly in the HTML its self.

You can use includes to include the same header on multiple pages, but that file could even be absent of any PHP.

If you are needing PHP to echo it for some reason, then the meta tag in question is just a string.
No different to either of these:

echo "How long is a piece of string?";
echo "<!DOCTYPE html>\

As i wrote: Oh wait. The header is something the server sends. So there is no need to prind it in the html head. I was thinking the metatag way.

Because x-ua was first a meta tag and not send by apache or now by php. So i was first thinking i need to put it in the head section.

IE 8/9 does so by default. It will run your site in standards mode as long as your site is standards compliant, ie., passes the HTML Validation and does not throw IE into quirks mode which it should not. Otherwise, you do not need it.

Call it prevention for the normal user. How many user know what the IE compatible mode is and realize if they maybe switched it on? Always make it luser save…

If the user turned compatibly mode on all the time, then this header is ignored. User choice prevails over the site.

But that means he/she is not a luser and knows what he/she is doing if they go into the IE options.

Then you have nothing to worry about because then IE will use the default which is standards mode.
Besides if the user switch compatibility mode on, in any case then it is on and your header is ignored.

The real purpose of the header is to turn compatibility mode on automatically for sites that need it.

You don’t get it. I talk about luser, for example like my parents. People who hit the comp mode button and did not realize that something is switched on that they don’t even know what it is.

It just force’s IE to use the most up to date rendering engine that it has available.

No I get it. But if they hit that button turning it on for every single site your header is ignored. The header does not do what you think it does. Its a suggestion not an absolute force this type deal.

IE 8/9 both use the most recent rendering engine they have BY DEFAULT. However, if the user wishes to change that they can, and your header is IGNORED. You cannot force the matter. The user can even chose to run in standard mode on websites that need compatibility mode. Again the header is ignored when overridden by the user.

It does, because if you send this header the button is not there. If its not there they can’t hit it.