FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Parse Error in PHP I can't figure out.





PatTheGreat42
Alright, I was sorta bored, so I decided to code up some sort of card game. It's really pretty stupid, but what's bothering me is that when I went to fire it up (http://www.patthegreat.com/cardgame.php, I got the following error:

Quote:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/patthegr/public_html/cardgame.php on line 64


I've been all over the code, and I really really can't find anything wrong with it. I suspected that I forgot to close a quote or a bracket or something, but I can't find any of those.

The code is here: http://www.patthegreat.com/cardgame.txt

I would really really appreciate if someone were to look that over and help me figure out what's going wrong. The error occurs at the big echo tag (echo "<img src=images/cards/$cards['0'...). Thanks so much!
MrBlueSky
Code:

!isset($cards['1')


should be

Code:

!isset($cards['1'])


in line 70

Also:

use
Code:
 echo "{$cards['0']}"


instead of
Code:
echo "$cards['0']"
PatTheGreat42
That bracket trick worked (thanks!), but what exactly does it do? I've never seen that before, and now I'm all curious.
RT Cunningham
I don't know why people keep posting that. It only serves to set apart the code in this instance. The curly braces are not required.
hexkid
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
MrBlueSky
RT Cunningham wrote:
I don't know why people keep posting that. It only serves to set apart the code in this instance. The curly braces are not required.


In this case they are required Exclamation
MrBlueSky
PatTheGreat42 wrote:
That bracket trick worked (thanks!), but what exactly does it do? I've never seen that before, and now I'm all curious.


BTW, here is the correct version of your script. I have changed some more things. See the comments.

Code:

<?php
$cards = chooseCards();
//
// use this instead of "if ($_GET['sent']"
//
if(isset($_GET['sent']))
{
   if($_POST['cardonecheck'])
   {
      if(ord($_POST['cardone']) == 0)
      {
         $value['0'] = substr($_POST['cardone'],1,1);
      }
      else
      {
         $value['0'] = substr($_POST['cardone'],0,2);
      }
   }
   if($_POST['cardtwocheck'])
   {
      if(ord($_POST['cardtwo']) == 0)
      {
         $value['1'] = substr($_POST['cardtwo'],1,1);
      }
      else
      {
         $value['1'] = substr($_POST['cardtwo'],0,2);
      }
   }
   if($_POST['cardthreecheck'])
   {
      if(ord($_POST['cardthree']) == 0)
      {
         $value['2'] = substr($_POST['cardthree'],1,1);
      }
      else
      {
         $value['2'] = substr($_POST['cardthree'],0,2);
      }
   }
   $total = $value['0'] + $value['1'] + $value['2'];
    // type error
   // if(gmp_nextprime($total - 1) == $toal)
    if(gmp_nextprime($total - 1) == $total)
   {
      $points = $_POST['points'] + $total;
   }
   else
   {
      $points = $_POST['points'];
   }
   $round = $_POST['round'] + 1;
   $score = floor($points/$round);
}
else
{
   $round=0;
   $score=0;
}
//
// use {$cards['0']}
//
echo "<img src=images/cards/{$cards['0']}.bmp>&nbsp;&nbsp;&nbsp;<img src=images/cards/{$cards['1']}.bmp>&nbsp;&nbsp;&nbsp;<img src=images/cards/{$cards['2']}.bmp>
<br><form action=cardgame.php?sent=true method=POST name=cardschosen>
<input type=checkbox name=cardonecheck>&nbsp;&nbsp;&nbsp;<input type=checkbox name=cardtwocheck>&nbsp;&nbsp;&nbsp;<input type=checkbox name=cardthreecheck>
<br><input type=submit value=Play>
<input type=hidden name=cardone value={$cards['0']}>
<input type=hidden name=cardtwo value={$cards['1']}>
<input type=hidden name=cardthree value={$cards['2']}>
<input type=hidden name=round value={$round}>
<input type=hidden name=points value={$points}>
</form>
<p>$score points.";
function chooseCards()
{
   $shapes = array("heart","spade","diamond","club");
   $values = array("null","null","02","03","04","05","06","07","08","09","10","11","12","13","14");
//
//
// Use 'do while' instead of while!
//
   do
   {
      $cards['0'] = $values[rand(2,14)].$shapes[rand(0,3)];
      $cards['1'] = $values[rand(2,14)].$shapes[rand(0,3)];
      $cards['2'] = $values[rand(2,14)].$shapes[rand(0,3)];
        //
        // Typo fixed
        //
   } while ($cards['0'] == $cards['1'] or $cards['0'] == $cards['2'] or $cards['1'] == $cards['2'] or !isset($cards['0']) or !isset($cards['1']) or !isset($cards['2']));
   return $cards;
}
?>
RT Cunningham
I stand corrected in this instance, but only after I saw the code.
qscomputing
RT Cunningham wrote:
I don't know why people keep posting that. It only serves to set apart the code in this instance. The curly braces are not required.


Code:
echo $test; //works
echo "test: $test"; //works
echo "$testtest"; //looks for $testtest rather than $test
echo "{$test}test"; //works as intended
echo "$test[1]"; //doesn't work IIRC because of array subscript
echo "{$test[1]}"; //works


The braces ensure that everything inside them - and just what is inside them - is interpreted as a variable.
Related topics
[PHP] Error link
PHP/Blogger error
Impossible parse error!
Trouble figuring out a parse error
Parse error: syntax error, unexpected '{' in /home/frih/publ
Php parsing error! Need help
php error ?[parse error]
Parse error - need help
PHP - Error
PHP Parse error on form handling - New to PHP
Parse error: syntax error, unexpected T_NEW
[PHP] Parse error
getting parse error when validating css
Cannot find parse error
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.