skip to Main Content

at the top of my functions.php I included another PHP file like the example below:

<?php  
  //file.php
  $var1 = "abc";
  $var2 = "123"

 ?>

In functions.php:


<?php   
include 'file.php';

// echoing or using the variable in functions.php returns a blank value
?>

In functions.php I included file.php but when I tried to echo $var1 it’s completely blank. Why does it not print "abc"?

2

Answers


  1. You should check the right path to the file you want to include.
    for example:

    include get_theme_file_path( '/subdir/filename.php' );
    
    Login or Signup to reply.
  2. You forgot to add semicolon at the end of the $var2. Please replace with below code.

    <?php  
      //file.php
      $var1 = "abc";
      $var2 = "123";
     ?>
    

    Also, please make sure that both the files in the same folder. If both he files in different folders, then please use below code to include the file:

    require get_template_directory() . '/folder-name/file.php';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search