Load a Header in wordpress

You’re really close – get_header_image() returns the URL of the image, whereas header_image() echo’s out the URL.

Because you’re setting your style tag in a variable and then echo’ing that, you need to use the function that returns the header image URL – so, like this:

$banner="style="background-image:url(". get_header_image() .');"';

Using header_image() will, as you found, echo out the URL directly, before you’re actually wanting to do so in your case.

Incidentally, a lot of WordPress functions follow this same naming scheme. Generally, a function starting with get_ will return the value, whereas a function starting with the_, or sometimes omitting it as in this case, will echo the value out.