tags:

views:

161

answers:

4

I'm trying to make a file that creates a array of 3 strings, than randomly displays one of the three 5 times. Can someone tell me what I'm doing wrong?

<?php

$pk[0] = "PK Fire!<br/>";
$pk[1] = "PK Thunder!<br/>";
$pk[2] = "PK Freeze!<br/>";

for($i = 0; $i < 5; $i++)
Echo "" + $pk[rand(0,2)] + "";

?>
+5  A: 

you need the "." to concatenate.

動靜能量
I don't understand. Please explain.
William
Bingo. The use of + to concatenate is essentially doing `echo 0+0+0;`.
ceejayoz
you are combining the string to form a larger one... in PHP, it is a dot instead of "+". If you remove the "" + and + "" part, it will work too.
動靜能量
an alternative is to use a comma: echo "", $pk[$something], "";
動靜能量
+10  A: 

. (dot) must be used instead of + to concatenate strings

Echo "" . $pk[rand(0,2)] . "";

instead of

Echo "" + $pk[rand(0,2)] + "";
Luc M
When I've been in javascript for too long my php sometimes gets this problem too :)
rpflo
+7  A: 

Jian Lin is correct, you're using "+" when you should be using "." to combine strings.

Echo "" + $pk[rand(0,2)] + "";

should become

echo "" . $pk[rand(0,2)] . "";

And really, you can just do:

echo $pk[rand(0,2)];

instead of concatenating blank strings before and after (which, as they're blank, add nothing).

ceejayoz
+1  A: 

By using the + operator, you're asking PHP to "coerce" all those variables into numeric values and add them up. Any thing that is not a number or a string that is a well-formed number will be converted to 0, and added together. Beware of this pitfall when comparing strings: use triple-equals rather than double-equals, lest you find that "fish" == "0".

Paul Fisher