14

PHP passing values ​​to variables in included files

 3 years ago
source link: https://www.codesd.com/item/php-passing-values-to-variables-in-included-files.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP passing values ​​to variables in included files

advertisements

I have a php file (article.php) with variables holding content and an include reference to a template holding the html structure (template.php).

<?php
$headline = 'Headline';
$author = 'Author';
$paragraph = 'Text';
include 'template.php';
?>

This is template.php noted above. It includes blockTemplate.php. (More on that, below.):

<html>
   <body>
      <?php include 'blockTemplate.php'; ?>
   </body>
</html>

This is blockTemplate.php. It has variables, the values to which I want to provide in article1.php, (the top level file):

<h1>Title: <?php $headline ?></h1>
<h2>Author: <?php $author ?></h2>
<p>Text: <?php $paragraph ?></p>

Here's the output I should be getting from article1.php:

Title: Headline

Author: Author

Text: text

Here's the output I am getting. Note the variables didn't get included:

Title:

Author

Text:

Question: how can I pass values in article1.php to the variables in included blockTemplate.php? Is what's happening the included files are being processed before inclusion so that contain no variables by the time they are included?


Have you tried:

<h1>Title: <?php echo $headline ?></h1>
<h2>Author: <?php echo $author ?></h2>
<p>Text: <?php echo $paragraph ?></p>

That works for me


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK