Michael Farah
Michael Farah

Reputation: 421

What is wrong with this PHP code - Unexpected T_CONSTANT

I keep getting the following error in my log

PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';'

The error is related to this line but I'm not sure what's wrong with it

<?php 
if ($num != null) {
    $query_string = 'msisdn=' . $num . '&ref=' . get_post_meta($post->ID, "ref", $single = true) ;
    echo '<div class="highlight"><a href="http://www.site.com/exJoin?signup=y&' .     htmlentities($query_string) . '"><b>Join Now</b></a></div>';
}
else{
    echo '<div class="highlight"><a href="<?php echo TeraWurflHelper::getPhoneHref('+2711111111'); ?>"><b>Join Now</b></a></div>';
}
?>

Upvotes: 0

Views: 241

Answers (4)

DaveRandom
DaveRandom

Reputation: 88697

Try this:

<?php 
   if ($num != null) {
     $query_string = 'msisdn=' . $num . '&ref=' . get_post_meta($post->ID, "ref", $single = true);
     echo '<div class="highlight"><a href="http://www.site.com/exJoin?signup=y&'.htmlentities($query_string).'"><b>Join Now</b></a></div>';
   } else {
     echo '<div class="highlight"><a href="'.TeraWurflHelper::getPhoneHref('+2711111111').'"><b>Join Now</b></a></div>';
   }
?>

Your problem was that you had a <?php echo... ?> in the middle of a string that was already being echoed. This had a ' in it, which was the type of quote used to encapsulate the string that was already being echoed. You could escape it (like \') but this would have your resulted in <?php echo... ?> being echoed into your HTML, which I doubt is what you want, instead your should remove this and put the function call into the middle of your echo.

This should be easy spot if you are using an editor/IDE with syntax highlighting. If your are not, look at EditPad, Notepad++ (editors) or Eclipse (IDE). Or Google it...

Upvotes: 0

JNDPNT
JNDPNT

Reputation: 7465

 else{echo '<div class="highlight"><a href="<?php echo     TeraWurflHelper::getPhoneHref('+2711111111'); ?>"><b>Join Now</b></a></div>';}

There lays the problem. You see the '+2711111111'. it uses " ' ". You'll have to escape that one, because it will end your string there.

Also, you do not need the opening tags for php in their... just remove them as you are already in a php-snippet.

Upvotes: 1

simnom
simnom

Reputation: 2620

Your adding PHP opening tags when you are already within a PHP tag. You should change to:

<?php
if ($num != null) {
    $query_string = 'msisdn='.$num.'&ref='.get_post_meta($post->ID, "ref", $single = true);
    echo '<div class="highlight"><a href="http://www.site.com/exJoin?signup=y&'.htmlentities($query_string).'"><b>Join Now</b></a></div>';
} else {
    echo '<div class="highlight"><a href="' . TeraWurflHelper::getPhoneHref('+2711111111') . '"><b>Join Now</b></a></div>';
}
?>

Upvotes: 1

Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46198

You are trying to echo a string containing <?php ?> tags

echo '<div class="highlight"><a href="<?php echo TeraWurflHelper::getPhoneHref('+2711111111'); ?>"><b>Join Now</b></a></div>';

Should propably be

echo '<div class="highlight"><a href="'.TeraWurflHelper::getPhoneHref('+2711111111').'"><b>Join Now</b></a></div>';

Upvotes: 0

Related Questions