Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add content to PHP include files?

Ok I may not have the best title , but I will try to give a better explanation.

Let's assume you are using PHP include() to structure your website:

Header.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name=keywords content="somthiefn"/>
        <title>Website</title>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <script src="Scripts/jquery-1.8.3.min.js"></script>
        <link rel="icon" type="image/png" href="/images/favicon.ico" />
    </head>
    <body>

Footer.php

    </body>
</html>

Then a sample page:

Index.php

<!--Header-->
<?php include ('includes/header.php'); ?>

   <div class="content">
       <!-- some content-->
   </div>

<!--Footer-->
<?php include ('includes/footer.php'); ?>

Basically I just want to know if there is a way to load some script into my header section.

I would like to achieve something like ASP.NET and its master Page, where I can just add content the header. for example

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Scripts/somescript.js"></script>
</asp:Content>
like image 767
meda Avatar asked Mar 18 '26 17:03

meda


2 Answers

Yes, you can do like this:

index.php

<?php
    // Wanted page title 
    $title = "some";

    // JS Files.. 
    $scripts = array();  
    $scripts[] = '<script src="js/some.js" />';
    $scripts[] = '<script src="js/some2.js" />';
    $scripts[] = '<script src="js/some3.js" />';

    // Include header 
    include ('includes/header.php');

header.php

<title><?php echo $title ?></title>
<?php echo implode("\n",$scripts) ?>

Of course the variables could be named as you want and contain any data. Main thing is that you can pass them between files like i showed.

like image 189
Hardy Avatar answered Mar 20 '26 06:03

Hardy


In index.php, before you include header.php, you could set an array for your scripts like:

header.php

<?php if(!isset($scripts)) $scripts = array(); ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name=keywords content="somthiefn"/>
        <title>Website</title>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <script src="Scripts/jquery-1.8.3.min.js"></script>
        <link rel="icon" type="image/png" href="/images/favicon.ico" />

        <!-- Include dynamic scripts-->
        <?php foreach($scripts in $script): ?>
        <script src="<?php echo $script; ?>"></script>
        <?php endforeach;?>

    </head>
    <body>

index.php

<?php
$scripts = array('Scripts/somescript.js', 'http://server.com/path/anoterscript.js);
?>

<!--Header-->
<?php include ('includes/header.php'); ?>

   <div class="content">
       <!-- some content-->
   </div>

<!--Footer-->
<?php include ('includes/footer.php'); ?>
like image 30
Ignacio Ocampo Avatar answered Mar 20 '26 06:03

Ignacio Ocampo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!