How can a PHP variable be copied to the clipboard by clicking a button without displaying the content?

To copy a PHP variable to the clipboard by clicking a button without displaying the content, you can use JavaScript to create a hidden textarea element, set its value to the PHP variable, select its content, and then copy it to the clipboard. This way, the variable content is never displayed on the page.

<?php
$variable = "Hello, World!";

echo '<button onclick="copyToClipboard(\'' . $variable . '\')">Copy to Clipboard</button>';

?>

<script>
function copyToClipboard(text) {
  var dummy = document.createElement("textarea");
  document.body.appendChild(dummy);
  dummy.value = text;
  dummy.select();
  document.execCommand("copy");
  document.body.removeChild(dummy);
  alert("Copied to clipboard: " + text);
}
</script>