0

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.

vicipi4669
  • 31
  • 4
  • I cannot reproduce this error. Are you sure though that these two files reside in the same folder on your server? – maio290 May 30 '22 at 16:25
  • code should be working... because ı tried code and worked,please open the error – dılo sürücü May 30 '22 at 16:28
  • @maio290 Yes, I'm sure. – vicipi4669 May 30 '22 at 16:28
  • @dılosürücü Any possibility causing this doesn't work? – vicipi4669 May 30 '22 at 16:28
  • 1
    Have you tried enabling error reporting as described [here](https://stackoverflow.com/a/21429652/4934937)? – maio290 May 30 '22 at 16:31
  • Avoid relative paths and use absolute paths where possible for example use `include __DIRNAME.'/data.php';` (if the files are in the same folder) – apokryfos May 30 '22 at 16:34
  • @apokryfos why? – Martin May 30 '22 at 16:35
  • As others have said; you should be looking at your PHP Error logs. [**Read here about how to find and read the PHP Error logs**](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php-5-apache-fastcgi-and-cpanel) . You should be logging errors and not outputting them to the user screen for best practise. – Martin May 30 '22 at 16:36
  • Does `var_dump(getcwd())` actually output the proper path? – maio290 May 30 '22 at 16:41
  • @vicipi4669 ok, so, are you showing us the code that actually populates that variable? If not please edit the code and show what's actually happening rather than a simplification. Cheers. – Martin May 30 '22 at 17:49
  • 1
    @maio290 accordingly to the update/edit the issue seems to not be the finding of the included file. – Martin May 30 '22 at 17:49
  • @Martin if your root script is in a different path, and that script is what includes `style.php` then this code won't work. If you use absolute paths it ensures the file is referenced correctly. – apokryfos May 31 '22 at 05:49
  • @apokryfos I do not disagree that Best Practise absolute pathing should always be used, but in **this question** the pathing for the include is not an issue because the OP (in their edit) does not state any "file not found" error which PHP would throw if an include/require function referenced an unfound file. – Martin May 31 '22 at 09:54
  • @vicipi4669 make sure all erorr reporting is enabled check [this question](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) to make sure you see everything that is happening – apokryfos May 31 '22 at 12:56

1 Answers1

0

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

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28