简体   繁体   中英

cannot access variable from another PHP

I got a PHP named data.php and it looks like this:

<?php
   $my_color = 'red';
?>

And I wanna use its value for a CSS property. The main PHP named style.php looks like this:

<?php
   header("Content-type: text/css; charset: UTF-8");
   include 'data.php';
?>

.test {
   background-color: <?php echo $my_color; ?>;
}

But this is not working because if I inspect it in the network of the developer tool, the value is actually blank and looks like this: background-color: ; . Anything I do it wrongly?

Edited : After looking at the error log, it shows: Undefined variable: my_color . But the file data.php and style.php are in the same folder. For example, the folder name is called plugin , and both files are in this folder, no other folder and file there.

run it from terminal

php -S 0.0.0.0:1234 index.php //be carefull s should be uppercase

Click the link http://0.0.0.0:1234 and, visit the page terminal will show you the error

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