Gandalf
Gandalf

Reputation: 13683

using internal css in php

I want to run the following code

echo "<html>
<head>
<style type="text/css">

p {color: white; }
body {background-color: black; }
</style>
</head>
<body>
<p>White text on a black background!</p>
</body>
</html>";
}

but this code don't run.However,when i run

echo "<html>
<head>
</head>
<body>
<p>White text on a black background!</p>
</body>
</html>";
}

the code runs well and

White text on a black background!

gets displayed.Am i including the internal css the right way?.

Thanks.

Upvotes: 1

Views: 6461

Answers (6)

Susam Pal
Susam Pal

Reputation: 34164

You have double-quotes in the string you want to echo. So, you need to escape them if you have double-quotes delimited string, i.e. you must replace all occurrences of " with \". You can avoid this escaping if you choose to delimit your string using single-quote rather than double-quote. Yes, you can have single-quoted strings in PHP. The only difference is that in single-quoted strings, variables are not expanded to its values and escape sequences lose their special meaning.

However, a neater way would be to use heredoc syntax as shown below.

<?php
echo <<<HTML
<html>
<head>
<style type="text/css">
p {color: white; }
body {background-color: black; }
</style>
</head>
<body>
<p>White text on a black background!</p>
</body>
</html>
HTML;
?>

An even better way to be just get out of PHP mode. e.g.

<?php
    // PHP code here.
?>
<html>
<head>
<style type="text/css">
p {color: white; }
body {background-color: black; }
</style>
</head>
<body>
<p>White text on a black background!</p>
</body>
</html>
<?php
    // PHP code here.
?>

Upvotes: 0

dtech
dtech

Reputation: 14060

You need to properly escape your strings, PHP now reads it as:

<?php
echo "<html><head><style type=";
text/css
"> p {color: white; } body {background-color: black; } </style>";
?>

Which obviously isn't correct PHP

You need to escape double quotes in your double quoted string, single quotes in your single quoted string or use NOWDOC/HEREDOC.

Valid examples:

<?php
echo "<style type=\"text/css\">";
echo "<style type='text/css'>";
echo '<style type="text/css">';   // This one minimizes escapes but still uses the standard double-quotes in HTML, I'd advise it
echo '<style type=\'text/css\'>';
echo <<<HTML
    <style type="text/css">
HTML;
?>

Upvotes: 0

bizzr3
bizzr3

Reputation: 1955

Better way (index.php) :

<html>
<head>
<style type="text/css">

p {color: white; }
body {background-color: black; }
</style>
</head>
<body>

<?php
//PHP Statements
?>

</body>
</html>

Upvotes: 2

Warren Sergent
Warren Sergent

Reputation: 2597

You are outputting using double-quotes, so the script dies when it hits the "text/css" as it assumes that you are finishing your double quote block.

Either put an escaping slash before your double quotes i.e. \"text/css\" or use single quotes i.e. 'text/css'

Upvotes: 1

Sjoerd
Sjoerd

Reputation: 75578

The problem is with the quotes in the style type="text/css". Because the string to echo is delimited by quotes, these quotes mess things up.

Either use single quotes ('), or escape the double quotes with backslashes (\").

Upvotes: 0

472084
472084

Reputation: 17885

You need to add a slash before your double quotes

echo "<html>
<head>
<style type=\"text/css\">
p {color: white; }
body {background-color: black; }
</style>
</head>
<body>
<p>White text on a black background!</p>
</body>
</html>";

Or use single quotes so they don't interfear with your HTML quotes;

echo '<html>
<head>
<style type="text/css">
p {color: white; }
body {background-color: black; }
</style>
</head>
<body>
<p>White text on a black background!</p>
</body>
</html>';

Or just dont use PHP?

?>
<html>
<head>
<style type="text/css">
p {color: white; }
body {background-color: black; }
</style>
</head>
<body>
<p>White text on a black background!</p>
</body>
</html>
<?php

Upvotes: 5

Related Questions