-1

Is it bad practice to write code which consists of php, javascript and ajax in the same piece of code? For example:

<?php
 $randNum = rand(1,10)
  if ($randNum <= 5) {
    echo "Enemy got the jump on you! <script> enemyTurn() </script>";
  }else {
    echo "You got the jump! <script> playerTurn() </script>";
  }
?>

enemyTurn(), playerTurn() are javascript functions which contain jQuery and AJAX.

2
  • You should be using mt_rand it's more random than the default libc rand and rougly 5x faster. Also, you could do this in Javascript without the need of PHP. Commented May 19, 2013 at 15:10
  • This will be a pain to maintain and debug. You should keep your code tidy and separated. Avoid situations when you use another language to render JavaScript (in most cases that's completely unnecessary). Commented May 19, 2013 at 15:32

3 Answers 3

1

It is, as it would be a bad practice to wash the dishes in the bathtub: you can still do it, but it will be a pain to have a shower when you need it.

Concerns should stay as decoupled as possible, an example would be:

PHP

<?php if ($randNum <= 5): ?>

<div data-function="enemyTurn">Enemy got the jump on you!<div>

<?php else: ?>

<div data-function="playerTurn">You got the jump!<div>

<?php endif; ?>

JS

$(function(){

  var yourFunctions = {

    enemyTurn : function(){ // some stuff },
    playerTurn : function(){ // some stuff }

  };

  $('[data-function]').each(function(){

    yourFunctions[$(this).data('function')].call(this);

  })

});

This way your js code is decoupled from HTML. Nevertheless I smell you can do all the logic of a game client side, using the server just for login / data save purposes.

Sign up to request clarification or add additional context in comments.

1 Comment

How is <div data-function="enemyTurn"> really different from <script>enemyTurn();</script>?
1

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?

4 Comments

Did you just answer his question with another question?
@phpNoOb In fact, I partially did. It's also a reasoning for the first part of my answer to be correct, though. If your application is designed to work if you do it that way, I cannot think of a reasonable other way to do it better without significatly overhauling the design of it.
Since I do this quite often, I would like to know if there ARE times at which it's not a good idea to mix all the three. This piece of code is just an example after all.
@frrlod This is a question on design. I added a different idea on how to design this, maybe it is useful to you in your future endeavors :)
0

You could achieve the same desired effect in Javascript with something like this:

if((Math.round(Math.random() * 10 + 1) % 2) === 0)
{
    enemyTurn();
    document.getElementById('status').innerHTML = 'Enemy got the jump on you!';
}
else
{
    playerTurn();
    document.getElementById('status').innerHTML = 'You got the jump!';
}

That way there's no need for unneeded server calls.

1 Comment

Now I just noticed your code was an example. You can ignore this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.