Skip to content Skip to sidebar Skip to footer

\n Breaking Php Code Within Javascript

When I run this code on my PHP website, it works but when adding a \n after 'text:' as php to make a new line, the entries in the table below the blue title at the top stop showing

Solution 1:

You actually need to escape the line-break twice. One escape for JavaScript and one for php

Your current string generates this php:

<?phpecho"This: 
breaks the code"; ?>

because the \n is evaluated by javascript into a linefeed character and this makes the php invalid.

You should use this string instead:

'<?phpecho"This: \\n does not break the code"; ?>'

Solution 2:

\n will not show in HTML. You have to coding like this.

function MyCtrl($scope) {
   $scope.environment_service_packages = 
[
  // &lt;br&gt; will be convert to <br>
  {name: 'obj1', info: {text: '<?phpecho"This: &lt;br&gt; breaks the code"; ?>', show: true}}, 
  {name: 'obj2', info: {text: 'some extra info for obj2', show: false}},
];
}

Post a Comment for "\n Breaking Php Code Within Javascript"