2

I have the following code, but the echo $jsonData[$i] line gives an error. I know you are not supposed to execute PHP within PHP. What is the correct way to accomplish this?

<?php
if( !is_user_logged_in() ){
   echo 'Im online. Login to Chat';
} else {
 echo '<input type=\"submit\" onclick=\"javascript:jqcc.cometchat.chatWith(' <?php echo $jsonData[$i]; ?> ');\" value=\"Chat Now\" class=\"success button small\"
\" >';
}
?>
5
  • php.net/manual/en/function.echo.php Commented Aug 25, 2016 at 2:36
  • 3
    This is not a bad question. Don't let down votes get to you. Commented Aug 25, 2016 at 2:37
  • This is a bad question because this is basic language feature. Sometimes, much more interesting questions get downvoted, because they are not following the site policy in asking questions. One of those policies is to ask after research, this question for example shows no research at all, since this is one of the first things that any PHP learner learns. Commented Aug 25, 2016 at 2:41
  • There are no bad questions. Only bad teachers. Commented Aug 25, 2016 at 2:46
  • Go to this page and put this string PHP within an Echo PHP in the title field and check the suggestions that the site suggests Commented Aug 25, 2016 at 2:59

2 Answers 2

6

You use the concatenation operator . instead of echo when you're already within the scope of a string:

echo '<input type="submit" onclick="javascript:jqcc.cometchat.chatWith(' . $jsonData[$i] . ');" value="Chat Now" class="success button small">';

Also, you don't need the backslashes unless you're using the same quotes to start and end the string. In your case you open/close with single quotes, so double quotes don't need to be escaped.

Sign up to request clarification or add additional context in comments.

Comments

0

Once you're in PHP mode (i.e. reach the opening <?php tag), everything is read as code until you exit PHP mode (i.e. reach the closing ?> tag). So there's no need to reopen those tags when they've already been open. See the basic syntax section of the manual for more details about this.

One of PHP's best features is that it allows you to embed code in HTML, rather than having to embed HTML in code. It's actually much easier to read your code when you write it like this, for example...

<input type="submit" onclick="javascript:jqcc.cometchat.chatWith(<?= $jsonData[$i]; ?>);" value="Chat Now" class="success button small">

Notice we only use PHP to print the value of $jsonData[$i] where it's needed and everything else is just plain/text or HTML that gets printed.

Also, note I'm using the short-hand form of <?php echo which is just <?=.


To expand on this a little, one of the reasons this idea of embeding code in HTML is so useful, is that it allows you to easily and transparently separate code from data.

<?php
$loggedIn = is_user_logged_in();

if ($loggedIn) {
?>
<input type="submit" onclick="javascript:jqcc.cometchat.chatWith(<?= $jsonData[$i]; ?>);" value="Chat Now" class="success button small">
<?php
} else {
?>
<h1>ohnoes, you're not logged in :(</h1>
<?php
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.