This seems more like a PHP programming question, but yes as you guessed in PHP if you want the value of a variable you just need $var
. echo $var
outputs it and will cause a syntax error how you’ve used it.
Otherwise everything else looks ok, although obviously I can’t say if that data structure will give you valid JSON-LD 😉
EDIT: Also it’s good to give your functions a more unique name than 'schema'
. You could call it 'yourname_jsonld_schema'
or something, so that it’s much more unique and unlikely to collide with any other names in WordPress
EDIT2: To address the extra error you added to your question, the place you define $schema
is inside an if
statement, which means that if the variable is not defined later, that if
was not true. So you need to do something like figure out why the if
is false, or if it’s correct, put the place where you use the $schema
inside the if
too.