This comes down to a question of design philosophy. If I was to write code like that, I'd probably have a class to add a message to, as well as a script call. It might look like this:
class BrowserAction {
public function __construct($message, $javascript) {
$this->message = $message;
$this->javascript = $javascript;
}
protected $message;
protected $javascript;
public function setMessage($message) {
$this->message = $message;
}
public function printMessage() {
return $this->message();
}
public function setJavascript($javascript) {
$this->javascript = $javascript;
}
public function printJavascript() {
return '<script type="text/javascript">' .
$this->javascript() .
'</script>';
}
public function toString() {
return $this->printMessage() . $this->printJavascript();
}
public function echo() {
echo $this->toString();
}
}
and use it like this:
<?php
$randNum = rand(1,10)
if ($randNum <= 5) {
$msg = new BrowserAction("Enemy got the jump on you!", "enemyTurn()");
}else {
$msg = new BrowserAction("You got the jump!", "playerTurn()");
}
$msg->echo();
?>
That would give you more flexibility in adding or extending your application later on. See how I simply added the type="text/javascript" to all scripts in the application by just inserting it in one place?
For this particular example, it's acceptable. In fact, what would you do otherwise to achieve the desired effect?
mt_randit's more random than the default libcrandand rougly 5x faster. Also, you could do this in Javascript without the need of PHP.