You only forgot quotations that are required for the JavaScript alert.
If you passed 'hello' to the function, your current code would create alert as:
alert(hello)
instead of doing:
alert("hello")
Therefore, change your code to the following (json_encode() the string and create a javascript variable out of it):
function died(string $message) {
if ('' === trim($message)) {
$message = 'died.';
}
?>
<script type="text/javascript">
var message = <?php echo json_encode($message); ?>;
alert(message);
</script>
<?php
die();
}
and you can use your function:
died('error on whatever');