Dirty Bird Design
Dirty Bird Design

Reputation: 5533

Returning PHP value to the page

I have a remote php program that generates 2 random numbers, I call to it on my form page to populate a text box used in validation. I can't get it to return the numbers.

<?php
    $randomNum = rand(0,9);
    $randomNum2 = rand(0,9);
    echo ($randomNum + $randomNum2);
    $randomNumTotal = $randomNum + $randomNum2;
?>

It is returning the total, not # + # Please help!

OK, thanks to the help below I got the output to be correct on the page. What I'm doing is the parent page brings in forms via AJAX. The forms are validated by a remote PHP script "random.php" in the forms there is a math problem for a somewhat human verification, the math problem is populated by the "random.php" file via .get command. Got that working. The issue now is that I can't solve the problem correctly... the answer input has this validation:

SomeName: {
    equal: "<? php echo $randomNumTotal ?>
}

but it's not working...any ideas?

Upvotes: 2

Views: 338

Answers (5)

jsw
jsw

Reputation: 2203

This might help: echo "$randomNum + $randomNum2 = " . $randomNum + $randomNum2;

Upvotes: 0

Clyde Lobo
Clyde Lobo

Reputation: 9174

You use the . in php for String concatenation

Try $randomNumTotal = $randomNum ."+". $randomNum2;

Upvotes: 4

Mark Elliot
Mark Elliot

Reputation: 77044

+ is an addition, while . is a string concatenation:

echo $randomNum . '+' . $randomNum2;

Upvotes: 1

Long Ears
Long Ears

Reputation: 4896

It sounds like you want to concatenate the digits rather than add them, which is . rather than +:

echo $randomNum . $randomNum2;

Upvotes: 2

Cristian Radu
Cristian Radu

Reputation: 8412

Use this:

echo $randomNum, ' + ', $randomNum2;

Upvotes: 1

Related Questions