How to pass arguments to an included file?

前端 未结 9 1933
無奈伤痛
無奈伤痛 2020-12-14 05:53

I\'m trying to make the whole section its own include file. One drawback is the title and description and keyword will be the same; I can\'t figure

相关标签:
9条回答
  • 2020-12-14 06:16

    index.php:

    <?php
    $my_header = 'aaaaaaaaaaaaaaaaaaaa';
    include 'header.php';
    ?>
    

    and header.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <link rel="shortcut icon" href="favicon.ico" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="Keywords" content=" <?php echo $my_header ?> " />
    <meta name="Description" content=" <?php echo $my_header ?> " />
    <title> <?php echo $my_header ?> </title>
    <link rel="stylesheet" type="text/css" href="reset.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    </head>
    

    It's not an ideal solution, but I understand it's your first steps in php.

    PS. Your Doctype doesn't match the code. I've adjusted your header html to be XHTML.

    0 讨论(0)
  • 2020-12-14 06:16

    You can't pass arguments to include, but it has access to all variables you've already set. From the include documentation:

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.

    Thus:

    index.php

    <?php
    $header = 'aaaaaaaaaaaaaaaaaaaaa';
    include("header.php");
    ?>
    

    header.php

    <title> <?php echo $header; ?> </title>
    
    0 讨论(0)
  • 2020-12-14 06:16

    Well marc, when you are using include, you can simply just set up a variable to use:

    <?php
      $var = "Testing";
      include("header.php");
    ?>
    

    In your header file:

    <?php
      echo $var;
    ?>
    

    Allow your previously defined variables are usable in any include you have.

    0 讨论(0)
  • 2020-12-14 06:20

    If you include a file it is just like inserting that code into the parent file. You could simply do this:

    <?php
    $parameter = "Hello World";
    include("header.php");
    ?>
    

    and then in the header.php

    <?php
    $parameter = isset($parameter) ? $parameter : "Default Text";
    // Use accordingly
    ?>
    

    I used the isset() method to verify that it has a value already and is instantiated.

    0 讨论(0)
  • 2020-12-14 06:22

    Include has the scope of the line it's called from.

    If you don't want to create new global variables, you can wrap include() with a function:

    function includeHeader($title) {
        include("inc/header.php");
    }
    

    $title will be defined in the included code whenever you call includeHeader with a value, for example includeHeader('My Fancy Title').

    If you want to pass more than one variable you can always pass an array instead of a string.

    Let's create a generic function:

    function includeFile($file, $variables) {
        include($file);
    }
    

    Voila!

    Using extract makes it even neater:

    function includeFileWithVariables($fileName, $variables) {
       extract($variables);
       include($fileName);
    }
    

    Now you can do:

    includeFileWithVariables("header.php", array(
        'keywords'=> "Potato, Tomato, Toothpaste",
        'title'=> "Hello World"
    ));
    

    Knowing that it will cause variables $keywords and $title to be defined in the scope of the included code.

    0 讨论(0)
  • 2020-12-14 06:22

    This is good approach. I however would do it a bit inside out. Define a layout, a wrapper for your webpage and include your content file into it:

    layout.phtml
    
    <html>
        <head>
          ... your headers go here
        </head>
        <body>
          <? include $content ?>
        </body>
    </html>
    

    Your content template file can look like this e.g.

    content.phtml
    
    <h1>hello world</h1>
    
    <p>My name is <?= $name ?></p>
    

    Then, you would have your main script (index) that will handle logic, connects to database etc.

    index.php
    
    $content = 'content.phtml';
    $name = 'Marc'; //Can be pulled from database
    
    include 'layout.phtml';
    

    This way, you can nicely separate business logic and presentation. And it can help you cut repetitive code for parts of page like logo or navigation which are repeated on the whole site.

    0 讨论(0)
提交回复
热议问题