Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Pass local variables to included php file/template

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #255950
    DaniSpringer.com
    Participant
    • I want to define a variable locally (on every page of a site)
    • Then create a HTML template with meta tags that use those variables
    • Then include it in every page, and fill it with the local variables.
    • Theoretical Example:

    about.php:

    <?php
    $title = "his is About page"
    include "links.php"
    ?>
    <html>
    <head>
    <?php meta_links(); ?>
    

    links.php:

    function meta_links()
    {
        echo "<title><?php $title; ?></title>"
    

    But when I try this, I get an error for the $title variable in links.php not being defined (which makes sense, I guess).

    How is this done?

    #255951
    Beverleyh
    Participant

    You need to pass a parameter. Google “PHP function parameter” for more info, but in brief;

    about.php

    <?php
    $title = "This is About page";
    include("links.php");
    ?>
    <html>
    <head>
    <?php echo meta_links($title); ?>
    

    links.php

    <?php
    function meta_links($my_title) {
        return $my_title;
    }
    ?>
    
    #255968
    DaniSpringer.com
    Participant

    @beverleyh thank you. Makes sense. I’ll try that when I get home.

    #255969
    DaniSpringer.com
    Participant
    • Looks like it worked! Thank you! (how do I accept answers here?)

    https://github.com/DaniSpringer/Projects/commit/1b96617fb970f23026d876f46c2b9dcb24ad023b

    #255970
    Beverleyh
    Participant

    Glad it helped.

    The green “Good answer!” is a form of answer acceptance here :)

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Other’ is closed to new topics and replies.