{"id":909,"date":"2017-03-20T10:05:53","date_gmt":"2017-03-20T04:35:53","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=909"},"modified":"2017-03-20T10:05:53","modified_gmt":"2017-03-20T04:35:53","slug":"php-code-to-include-css-in-a-php-file","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-code-to-include-css-in-a-php-file\/","title":{"rendered":"PHP &#8211; Code to include css in a .php file"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM<\/label><\/p>\n<p>To include a css file to .php file.<\/p>\n<p>Inside the .php file, We have;<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%09Loads%20of%20code..%0A%09%3F%3E\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>We want it like this..<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%09%3Clink%20href%3D%22bootstrap%2Fcss%2Fbootstrap-responsive.css%22%20rel%3D%22stylesheet%22%3E%0A%09Loads%20of%20code..%0A%09%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>But obviously that doesn\u2019t work. Is there any way to do this?<\/p>\n<p><strong>There are Four ways to solve this :<\/strong><\/p>\n<p>Solution 1 : echo a snippet.<\/p>\n<p>Solution 1 : include\/require<\/p>\n<p>Solution 2: same as you normally do in a HTML file.<\/p>\n<p>Solution 3:Consider your CSS file as a PHP file.<\/p>\n<p>PHP is a server-side language,CSS is client-side and is used by the browser.<\/p>\n<p><label class=\"label label-info\">SOLUTION 1<\/label><\/p>\n<p><strong>use echo :<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%09echo%20\u2019%3Clink%20href%3D%22bootstrap%2Fcss%2Fbootstrap-responsive.css%22%20rel%3D%22stylesheet%22%3E\u2019%3B%0A%09%3F%3E\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 2<\/label><\/p>\n<p><strong>Use include\/require :<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3Cstyle%3E%3C%3F%20include_once%20%E2%80%9DPATH%2Fyour_style.css%22%20media%20%3D%22%22%20%20%3F%3E%3C%2Fstyle%3E%0A%2F%2F%0A%3Cstyle%3E%3C%3F%20require_once%20%E2%80%9DPATH%2Fyour_style.css%22%20media%20%3D%22%22%20%3F%3E%3C%2Fstyle%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 3<\/label><\/p>\n<p><strong>same as you normally do in a HTML file.<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3F%0A%3Chead%3E%0A%3Clink%20href%3D%E2%80%9DPATH%2Fyour_style.css%E2%80%9D%20%3E%0A%3C%2Fhead%3E%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 4<\/label><\/p>\n<p><strong>Consider your CSS file as a PHP file.<\/strong><\/p>\n<p>( instead of your_style.css, work with your_style.php )<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3F%0A%20header(%22Content-type%3A%20text%2Fcss%3B%20charset%3A%20UTF-8%22)%3B%0A%20%0A%24style_tipo%20%3D%20%22Helvatica%2C%20Arial%2C%20sans-serif%22%3B%0A%24style_colour%3D%20rgba(255%2C%20255%2C%20255%2C%2030)%3B%0A\u2026.%0A%20%0Aa%20%7B%20color%3A%20%3C%3F%20echo%20tyle_colour%20%3F%3E%20%7D%0A\u2026.%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Css file linking can be categorized in two ways :<\/p>\n<p>\u00a0<\/p>\n<p><label class=\"label label-info\">SOLUTION 5<\/label><\/p>\n<p><strong>By Html Code :<\/strong><\/p>\n<p>This is the traditional method for including CSS in your document.<br \/>\nIt can be done by using link tag in head section of the document.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3Clink%20rel%3D%22stylesheet%22%20href%3D%22main.css%22%20type%3D%22text%2Fcss%22%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 6<\/label><\/p>\n<p><strong>By Php Code :<\/strong><\/p>\n<p>This is by far new and powerful way to import css in a document.<\/p>\n<p>But the only condition is you have to surround the php code with style element.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%20include%20\u2019header.php\u2019%3B%20%3F%3E%3Cstyle%3E%3C%3Fphp%20include%20\u2019CSS%2Fmain.css\u2019%3B%20%3F%3E%3C%2Fstyle%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>CSS files aren\u2019t linked to PHP, they are linked to HTML.<\/p>\n<p><strong>PHP can generate a line of HTML that references a CSS file like this:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3Clink%20rel%3D%22stylesheet%22%20type%3D%22text%2Fcss%22%20href%3D%22my-style.css%22%3E%0A\u201d message=\u201dphp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 7<\/label><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%0Aecho%20\u2019%3Chead%3E\u2019%3B%0A%20%20%20%20echo%20\u2019%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22%2Fstyles.css%22%20type%3D%22text%2Fcss%22%3E\u2019%3B%0Aecho%20\u2019%3C%2Fhead%3E\u2019%3B%0A%0Arequire(%20%22config.php%22%20)%3B%0A%0A%24Quote%20%3D%20new%20CQuote%3B%20%0A%0Aif(%20%24Quote-%3EGet(%20%24strQuote%20)%20%3D%3D%200%20)%0A%09echo%20%24strQuote%3B%0A%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM To include a css file to .php file. Inside the .php file, We have; [pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%09Loads%20of%20code..%0A%09%3F%3E\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] We want it like this.. [pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%09%3Clink%20href%3D%22bootstrap%2Fcss%2Fbootstrap-responsive.css%22%20rel%3D%22stylesheet%22%3E%0A%09Loads%20of%20code..%0A%09%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] But obviously that doesn\u2019t work. Is there any way to do this? There are Four ways to solve this [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1731,1732,1724,1720,1719,1726,1725,1722,1717,1723,1718,1721,1729,1730,1727],"class_list":["post-909","post","type-post","status-publish","format-standard","hentry","category-php","tag-css-in-php-tutorial","tag-css-not-working-in-php-file","tag-how-do-i-correctly-link-my-css-file-to-both-my-index-php-and-my-include-php-files","tag-how-do-we-link-css-files-to-a-php-file","tag-how-to-add-css-to-php-file","tag-how-to-include-css-in-php-echo","tag-how-to-include-external-css-file-in-php","tag-how-to-include-style-sheet-as-php-file","tag-including-css-in-php-file","tag-including-external-javascript-and-css-files-in-php","tag-linking-a-css-file-to-a-php-script","tag-php-5-include-and-require","tag-php-css-examples","tag-php-include-css-not-working","tag-php-include-css-path"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=909"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/909\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}