Hello,
Please check the documentation available on https://www.phpdocx.com/documentation/cookbook/replace-placeholders-targets. The target option allows setting a single value, but you can replace all targets adding just two lines:
foreach (array('document', 'header', 'footer', 'footnote', 'endnote', 'comment') as $target) {
$docx->replaceVariableByText($variables, array('target' => $target));
}
Regards.