4

For instance, in javascript: 1

<script>
var name = "<?php echo 'Adam'; ?>";
alert("The name is: " + name);
</script>

or 2

<script>
alert("The name is: <?php echo 'Adam'; ?>");
</script>

And in html? 1

<head>
<?php echo '<title>Page #1</title>'; ?>
</head>

or 2

<?php $page_number = "1"; ?> // on top of the script with the rest of the other X code. //
<!doctype html....
<html>
<head>
<title>Page #<?php echo $page_number; ?></title>
</head>

I'm not looking for the short, long, easy or hard method, I'm looking for the correct method to do this.

1 - In a professional way, how this should be done?

2 - What is your way/method to do this?

Thanks

Update: Also:

<input type="button" onclick="javascript:add_friend(0,<?php echo $profile_user_id; ?>);" />

1 Answer 1

2

There is no correct method. Rather, there are conventions that you will find yourself using, or your team will find itself using. Further, it is at times a case by case situation where your HTML may need to be processed a bit by PHP prior to being spit out, and other times it may not.

I happen to prefer seeing as clean as separation as possible, where the limits of the PHP tags are shared by the same limits of any code we would hard-wire into place.

<title><?php echo $title; ?></title>
<script>
  var strValue = <?php echo json_encode( $strValue ); ?>;
</script>

But again, that's just me.

With regards to JavaScript within HTML attributes, I would encourage you to stay as far away from that as you can. Rather than using onclick attributes, bind all of this up in your JavaScript. This, again, keeps a clean line of separation between your structure and your logic.

Which would you prefer seeing in your markup:

<input type="button" onclick="javascript:add_friend(0,288);" />

Or

<input type="button" data-action="add_friend" data-friendId="288" />

Keep your programmers out of your design, and more importantly your designers out of your code.

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

2 Comments

+1 I agree with your point of view. Echoing tags is completely unnecessary (and not much IDE-friendly), so this should be treated as a last resort. Also when passing PHP variables to JavaScript during page rendering, I prefer assigning their values to some JavaScript variables, or calling functions/methods that are created for that, or assigning variables to data- attributes of HTML elements, if they are somehow associated with HTML elements. But definitely not including PHP echo calls within JavaScript - one of the reasons is the problem with moving such script outside, to separate JS file.
For inserting onto JS, you SHOULD use json_encode, otherwise all bets are off if the generated data is actually synatically valid JS valid or not.

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.