0

Its been a long time since I have done php, Im trying to set a var with a array inside then call that var within a array.. now im typing it seems wrong..

I get "undefined var error" anyone know what i doing wrong here ?

$myvar = array(
    'templates/page-one.php',
    'templates/page-two.php');

function se_remove_styles() {
    if ( is_page_template( [$myvar]  ) ) {
...
}
}
Beep
  • 2,737
  • 7
  • 36
  • 85
  • try reading up on [scope](https://www.php.net/manual/en/language.variables.scope.php) :) – treyBake Jul 19 '19 at 12:34
  • @peter possibly yes, however still getting the error – Beep Jul 19 '19 at 12:36
  • 1
    Look for the 'Extending the scope of variables into anonymous functions' part in the linked answer. So it should be like this: `function se_remove_styles() use($myVar) {` `if ( is_page_template( $myvar ) ) {` – Exceptional NullPointer Jul 19 '19 at 12:37
  • @Beep because $myVar is not in function scope.. hence the link/dupe – treyBake Jul 19 '19 at 12:38
  • @treyBake im with you, thought it may be something like it needs to be within the function, so how to set it as a global var so that I can use in multiple functions – Beep Jul 19 '19 at 12:39
  • 1
    I updated my comment here with the specific part – Exceptional NullPointer Jul 19 '19 at 12:40
  • Inside the function, just add `global $myvar;` as the first line. – nice_dev Jul 19 '19 at 12:41
  • 1
    ^^although that works, using globals is hardly ever the answer. Creating a class with a __construct method to set vars is a much better practice. – treyBake Jul 19 '19 at 12:43
  • I see, perfect thanks guys, been working with JS so long now basic PHP knowledge is gone. I know it was a stupid question thanks for the help in getting this working – Beep Jul 19 '19 at 12:45

0 Answers0