markie markie - 2 months ago 6
PHP Question

PHP - include/require/insert some data from one file to another

I'm not really familiar with php and would like to ask you to help me.

I want to insert

css
styling data from
styles.css
file into the
<style>
tag inside of
index.php
, and not sure how to do so with secure and optimized way.

Should I use
<?php include 'styles.css;?>
or
<?php require 'styles.css;?>
or what exactly should I do in order to insert the data in the optimized and secure way, please?

Answer

require/include is for parsing a PHP file, not copying content.

Just read the file and echo the contents.

<?= file_get_contents('styles.css') ?>

where <?= is a shorthand for <?php echo. See also: HTML output from web server

Note that the include path in PHP environment may be different from that in the HTML, as understood by the browser. Better use __DIR__ . '/styles.css' to prevent problems.