Get declared variable in single.php to work in comments.php without re-declaring it

I have declared a variable in my single.php

$title  = 'myvar';

Can I get it in my comments.php without re-declaring it again?

Read More

I use this code to get the comments.php templage:

<? comments_template( '', true );  ?>

Ty

Related posts

Leave a Reply

2 comments

  1. The comments_template() only accepts two parameters: $file (string) and $separate_comments (Boolean). So, it does not have a way to pass an arbitrary variable as a parameter.

    The two methods I generally use are:

    1. Globalize the variable
    2. Wrap the variable inside a function that returns the data you want, and then call it wherever you need it.
  2. You can use php session variables to utilize the myvar variable anywhere you want.

    if(!$_SESSION['myvar']){
    
        $_SESSION['myvar'] = 'myvar';
    
    }
    
    echo $_SESSION['myvar'];