简体   繁体   中英

In PHP how can access a property in an included file that was defined in the calling file?

If I have two files

main.php

<?php
    $title = "thisTitle";
    include $_SERVER['DOCUMENT_ROOT']."/aatemplate.php";
?>

aatemplate.php

<html lang="en">
    <head>
        <?php include $_SERVER['DOCUMENT_ROOT']."/common/head.php";?>
        <title>$title</title>
    </head>
</html>

How can I make the '$title' in the template file be replaced with the $title set in the main.php so that it becomes:

<title>thisTitle</title>

If you include a file that's will be access to main file & previous included files variables.

Change

<title>$title</title>

to

<title><?php echo $title; ?></title>

You can refer to PHP variables that are declared by using the echo statement

Replace with <?php echo $title; ?> <?php echo $title; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM