Very Weird Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Very Weird Error

Post by Ree »

When I use this

Code: Select all

$replacer->ReplaceGlobalValue('ItemValue', "<?php echo $row['headline']; ?>");
I get this

Code: Select all

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Software\Apache2\htdocs\inter\replacer\definers\replace_del_news.php on line 7
How come? I have singe quotes between double ones.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Very Weird Error

Post by Roja »

Ree wrote: How come? I have singe quotes between double ones.
Cause you are already in php mode at the beginning of the line. It should be:

Code: Select all

$replacer->ReplaceGlobalValue('ItemValue', $row['headline']);
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Re: Very Weird Error

Post by Ree »

Roja wrote:
Ree wrote: How come? I have singe quotes between double ones.
Cause you are already in php mode at the beginning of the line.
hahaha :lol: . no, I need the string to be "<?php echo $row['headline']; ?>". I want to pass the string, not the value of $row['headline'].
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Try using single quotes...

Code: Select all

$replace=$replacer->ReplaceGlobalValue('ItemValue', '<?php echo $row["headline"]; ?>');
Double quotes allow variables to be replaced with their values so the $row['headline'] is probably causing the error.

Hope that solves it.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Yes, I had this solution in mind as well and now the string gets passed, but is it ok to use double quotes when retrieving an array element? I've always used single quotes for string keys and no quotes for numeric keys.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Convention is to always use single quotes for arrays but I think it is an acceptable exception in this case to make the thing more readable without having to concatenate different things.

Code: Select all

$replace=$replacer->ReplaceGlobalValue('ItemValue', '<?php echo $row['."'"headline'".']; ?>');
is 8O
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

I think you have misunderstood him coder. While your method may be correct (although I'm not sure, never really tried it that way... ?), I think he was meaning something like this :

Code: Select all

//Double Quotes Array for $row
$replace=$replacer->ReplaceGlobalValue('ItemValue', '<?php echo $row["headline"]; ?>');

//Single Quote Array for $row
$replace=$replacer->ReplaceGlobalValue('ItemValue', '<?php echo $row['headline']; ?>');

both are very valid and it's all about your choice of preference. It pretty much falls in the same like of echoing with single or double quotes. Both work the same except double quotes will allow you a little more control over the string should you decide you need to insert new line characters or the like.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

//Single Quote Array for $row
$replace=$replacer->ReplaceGlobalValue('ItemValue', '<?php echo $row['headline']; ?>');
Will not work as you are closing the quoted string (guess T_STRING error). Bear in mind he wanted the string, not the value. Double quotes work for arrays although another solution would be to escape the $ with a backslash and use double quotes around the whole thing.

Code: Select all

$replace=$replacer->ReplaceGlobalValue('ItemValue', "<?php echo \$row['headline']; ?>");
Now why didn't I think of that before... :oops:
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

the things you explain work, yes. now i'm getting mad while trying to solve a more complex thing.

Code: Select all

echo '

<tr>
  <td><a href="view_news.php?item=<?php echo $row['id']; ?>"><?php echo $row['headline']; ?></a></td>
  <td class="padding_side"><?php echo $row['posted']; ?></td>
  <td><input type="checkbox" name="item_array[]" value="<?php echo $row['id']; ?>" /></td>                    
</tr>

';
simply won't work. I've been trying to solve the same but without any luck. Any ideas on how to make the thing echo'able?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

infolock wrote:

Code: Select all

//Single Quote Array for $row
$replace=$replacer->ReplaceGlobalValue('ItemValue', '<?php echo $row['headline']; ?>');
both are very valid and it's all about your choice of preference. It pretty much falls in the same like of echoing with single or double quotes. Both work the same except double quotes will allow you a little more control over the string should you decide you need to insert new line characters or the like.
Actually that one is not valid, you forgot to escape your single quotes :wink:

Code: Select all

//Single Quote Array for $row
$replace=$replacer->ReplaceGlobalValue('ItemValue', '<?php echo $row[\'headline\']; ?>');
Ree wrote:the things you explain work, yes. now i'm getting mad while trying to solve a more complex thing.

Code: Select all

echo '

<tr>
  <td><a href="view_news.php?item=<?php echo $row['id']; ?>"><?php echo $row['headline']; ?></a></td>
  <td class="padding_side"><?php echo $row['posted']; ?></td>
  <td><input type="checkbox" name="item_array[]" value="<?php echo $row['id']; ?>" /></td>                    
</tr>

';
simply won't work. I've been trying to solve the same but without any luck. Any ideas on how to make the thing echo'able?
Read my reply to infolock
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Really, I cannot get this echoed properly. I tried escaping quotes, but then I get no values that should be there.

Code: Select all

echo '

<tr>
  <td><a href="view_news.php?item=$row['id']">$row['headline']</a></td>
  <td class="padding_side">$row['posted']</td>
  <td><input type="checkbox" name="item_array[]" value="$row['id']" /></td>
</tr>

';
I really need some help here, how can I make the above echoed properly (properly I mean values of $row array printed)? Escaping like this

Code: Select all

$row[\'posted\']
prevents outputting the values.
Last edited by Ree on Wed Sep 07, 2005 3:43 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

echo '

<tr>
  <td><a href="view_news.php?item='.$row['id'].'">'.$row['headline'].'</a></td>
  <td class="padding_side">'.$row['posted'].'</td>
  <td><input type="checkbox" name="item_array[]" value="'.$row['id'].'" /></td>
</tr>

';
or

Code: Select all

echo <<<STOP


<tr>
  <td><a href="view_news.php?item={$row['id']}">{$row['headline']}</a></td>
  <td class="padding_side">{$row['posted']}</td>
  <td><input type="checkbox" name="item_array[]" value="{$row['id']}" /></td>
</tr>


STOP;
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Finally got it working. Thanks for ideas :)
Post Reply