What is wrong with this PHP code - Unexpected T_CONSTANT - php

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"><b>Join Now</b></div>';
}
else{
echo '<div class="highlight"><b>Join Now</b></div>';
}
?>

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"><b>Join Now</b></div>';
} else {
echo '<div class="highlight"><b>Join Now</b></div>';
}
?>

else{echo '<div class="highlight"><b>Join Now</b></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.

Try this:
<?php
if ($num != null) {
$query_string = 'msisdn=' . $num . '&ref=' . get_post_meta($post->ID, "ref", $single = true);
echo '<div class="highlight"><b>Join Now</b></div>';
} else {
echo '<div class="highlight"><b>Join Now</b></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...

You are trying to echo a string containing <?php ?> tags
echo '<div class="highlight"><b>Join Now</b></div>';
Should propably be
echo '<div class="highlight"><b>Join Now</b></div>';

Related

How do you string a variable together along with a string in PHP?

So I have a variable in PHP that I would like to print along with an html tag but I am very new to PHP and I can't seem to get the syntax right
My goal is to output/print the following
<div class="post-block" style="background-image:url(Whatever image here);">
My code is below:
$feature_posts = the_sub_field('feature_image_post');
if ( get_sub_field('post-feature') == "" ) {
echo '<div class="post-block" style="background-image:url(' .$feature_posts");>';
echo '</div>';
}
You are missing the concatenation operator and the closing single quote after your variable:
$feature_posts = the_sub_field('feature_image_post');
if ( get_sub_field('post-feature') == "" ) {
echo '<div class="post-block" style="background-image:url(' . $feature_posts . ')">';
echo '</div>';
}
To keep the format more readable you can use double quotes to allow variables in your string. Just have to remember escaping the quotes within the markup:
$feature_posts = the_sub_field('feature_image_post');
if ( get_sub_field('post-feature') == "" ) {
echo "<div class=\"post-block\" style=\"background-image:url($feature_posts)\">";
echo '</div>';
}
You can make that.
<?php for($i = 0; $i <= 100; $i++){ ?>
<div class="post-block" style="background-image:url(<?php echo the_sub_field('feature_image_post') ?>);"></div>
<?php } ?>
This a sample how you can do.

How to add variable in error message (php) multilanguage?

Need error message in two languages, and have some trouble with single quotes...
This is how it looks like:
else if(trim($FullName) == '')
$error = '<div class="errormsg">Please enter your Full Name!</div>';
}
And how when I put variable
else if(trim($FullName) == '')
$error = '<div class="errormsg"><?php echo $lang['error']; ?></div>';
}
So, when I put it like this, syntax becomes incorrect because single quotes..
did u try
else if(trim($FullName) == '')
$error = '<div class="errormsg">'. $lang["error"] .'</div>';
}
EDIT : echo is a php instruction that does not require you to re-open and re-close php tags (<?php and ?>). You can compute strings thanks to concatenation which is some basic stuff in PHP
I would use empty, i.e.:
else if(empty(trim($FullName)))
$error = '<div class="errormsg">Please enter your Full Name!</div>';
}
the following, doesn't make sense since you're inside a php block:
$error = '<div class="errormsg"><?php echo $lang['error']; ?></div>
Change for:
else if(empty(trim($FullName)))
$error = '<div class="errormsg">'.$lang['error'].'</div>';
}

Avoiding line breaks in HTML output of PHP

Here I have my print function:
<?php
// little helper function to print the results
function printTag($tags) {
foreach($tags as $t) {
echo '<span class="' . $t['tag'] . '">';
echo $t['token'] . "/" . $t['tag'];
echo '</span>';
echo " ";
}
}
$tagger = new PosTagger('lexicon.txt');
?>
And here is what I'm outputting from an HTML form:
<?php
if($_POST['submitbutton'] == "Submit") {
//Check whether the form has been submitted
$tags = $tagger->tag($_POST['texttotag']);
printTag($tags);
}
?>
My problem is, the output in the browser results in strange line breaks in the middle of some of my <span> like so:
<span class="VB">Enter/VB</span> <span class="PRP$">your/PRP$</span> <span class="NN
">text/NN
</span> <span class="TO">to/TO</span> <span class="NN">tag/NN</span> <span class="RB
">here/RB
</span>
This means my CSS definitions don't apply to the "interrupted" spans. Any idea why this is happening and how I can stop it? I've had a good look around and haven't been able to find cause/solution. Thanks.
It seems that your $t['tag'] variable includes a line-break.
You can get rid of that using trim($t['tag']) instead.
It appears that the $t['tag'] in printTag function contains line break character. You can use str_replace function to remove these characters like:
function printTag($tags) {
foreach($tags as $t) {
echo '<span class="' . $t['tag'] . '">';
$ttag = str_replace('\n','',$t['tag']);
$ttag = str_replace('\r','',$ttag);
echo $t['token'] . "/" . $ttag;
echo '</span>';
echo " ";
}
}

$_GET OR escape string being unpredictable. Please explain?

i simply pass 3 values to the URL and while testing i was trying to echo them back to the screen but it will only echo each value once even though i have set it to echo at various points. once i escape the value it wont let me echo it. Why is this?
<?php
session_start();
if (isset($_SESSION['SESSION_C']) && ($_SESSION['SESSION_C']==true))
{
$getyear = $_GET["Year"];
echo $getyear; (IT WILL ECHO AT THIS POINT)
$getyear = mysql_real_escape_string($getyear);
echo $getyear; (BUT WONT ECHO HERE)
$getsite = $_GET["Site"];
echo $getsite;
$getsite = mysql_real_escape_string($getsite);
echo $getsite;
$getsite = str_replace(' ', '', $getsite);
echo $getsite;
$getdoc = $_GET["Doc"];
echo $getdoc;
$getdoc = mysql_real_escape_string($getdoc);
echo $getdoc;
}
else
{
echo "sessionerror";
}
?>
mysql_real_escape_string() requires a open connection to mysql. Otherwise it will return false. I guess var_dump($getdoc); will give you boolean(false).
You'll have to call mysql_connect() before that code.

Call PHP script inside a function via return

I am working on integrating two wordpress plugins. What I am trying to do is to add this code<?php DisplayStars(get_the_ID()); ?> into a function of another plugin. I tried $html = '<?php DisplayStars(get_the_ID()); ?>';, but php shows errors. Thanks for your help.
function wpbusdirman_post_excerpt($count)
{
$wpbusdirman_gpid=wpbusdirman_gpid();
$wpbusdirman_permalink=get_permalink($wpbusdirman_gpid);
$html = '';
$html .= '<div id="wpbdmlistings"';
$isasticky = get_post_meta(get_the_ID(),'sticky');
if(isset($isasticky) && !empty($isasticky))
{
$isasticky=$isasticky[0];
}
if(isset($isasticky) && ($isasticky == 'approved'))
{
if($count&1)
{
$html .= ' class="wpbdmoddsticky"';
}
else
{
$html .= ' class="wpbdmevensticky"';
}
}
else
{
if($count&1)
{
$html .= ' class="wpbdmodd"';
}
else
{
$html .= ' class="wpbdmeven"';
}
}
$html .='><div class="listingthumbnail">' . wpbusdirman_display_the_thumbnail() . '</div><div class="listingdetails">';
$html .= wpbusdirman_display_the_listing_fields();
$html .= wpbusdirman_view_edit_delete_listing_button();
$html .= '</div><div style="clear:both;"></div></div>';
return $html;
}
The code base you're working in is php. The code you're adding is php. That means, you don't need to add it inside the html, but simply call the function. (You may need an include at the top of the file for the function to work if it's not in this file)
$html .= DisplayStars(get_the_ID());
should be all you need in the call to add the text to the html.
When you do something like this:
<?php return "<?php echo('foo'); ?>"; ?>
Then PHP doesn't know (or care) about what is between the quotation marks - it's just text, as far as it knows. However, you can get PHP to interpret a string by using eval:
<?php
$command = "echo('foo');";
eval($command);
?>
This will cause PHP to print out "foo". But watch out, eval is dangerous - see the note in the PHP manual.

Categories