Fishcakes Posted April 4, 2021 Share Posted April 4, 2021 Hi I am trying to use the nl2br function like this while ($row = mysqli_fetch_array($query)) { //May need this later to output pictures // $imageURL = 'upload/'.rawurlencode($row["filename"]); echo " <div class='divTableRow'> <div class='divTableCell'>{$row['User']} ;</div> <div class='divTableCell'>nl2br({$row['CommentText']});</div> </div> \n"; } However the output just looks like the attached picture. When I check in the sql db I can see the line breaks when doing a select * from Table ; Quote Link to comment https://forums.phpfreaks.com/topic/312422-using-nl2br-function-inside-while-and-echo/ Share on other sites More sharing options...
Barand Posted April 4, 2021 Share Posted April 4, 2021 You cannot place a function call in a string and expect it to be executed - it just becomes part of the string. Instead you need to concatenate. For example echo "<div class='divTableCell'>" . nl2br($row['CommentText']) . "</div>"; Quote Link to comment https://forums.phpfreaks.com/topic/312422-using-nl2br-function-inside-while-and-echo/#findComment-1585589 Share on other sites More sharing options...
Fishcakes Posted April 4, 2021 Author Share Posted April 4, 2021 Yes I tried this using single quotes which just outputs it as a string <div class='divTableCell'> ' . nl2br({$row['CommentText']}) . ';</div> And if I use double quotes as you've done it causes my page not to load while ($row = mysqli_fetch_array($query)) { //May need this later to output pictures // $imageURL = 'upload/'.rawurlencode($row["filename"]); echo " <div class='divTableRow'> <div class='divTableCell'>{$row['User']} ;</div> <div class='divTableCell'> " . nl2br({$row['CommentText']}) . ";</div> </div> \n"; } Quote Link to comment https://forums.phpfreaks.com/topic/312422-using-nl2br-function-inside-while-and-echo/#findComment-1585590 Share on other sites More sharing options...
Fishcakes Posted April 4, 2021 Author Share Posted April 4, 2021 I also tried declaring the variable ($testvar) outside the echo statement but the page still won't load even then while ($row = mysqli_fetch_array($query)) { //May need this later to output pictures // $imageURL = 'upload/'.rawurlencode($row["filename"]); $testvar = nl2br({$row['CommentText']}) ; echo " <div class='divTableRow'> <div class='divTableCell'>{$row['User']} ;</div> <div class='divTableCell'> $testvar ;</div> </div> \n"; } echo $_SESSION['id'] ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/312422-using-nl2br-function-inside-while-and-echo/#findComment-1585591 Share on other sites More sharing options...
dodgeitorelse3 Posted April 4, 2021 Share Posted April 4, 2021 check your semi colons ( ; ) Quote Link to comment https://forums.phpfreaks.com/topic/312422-using-nl2br-function-inside-while-and-echo/#findComment-1585592 Share on other sites More sharing options...
Barand Posted April 4, 2021 Share Posted April 4, 2021 $testvar = nl2br({$row['CommentText']}) ; ^ ^ and remove the curly braces. Turn on php error reporting, that line above should give Quote Parse error: syntax error, unexpected '{' in C:\inetpub\wwwroot\test\noname28.php on line ... Quote Link to comment https://forums.phpfreaks.com/topic/312422-using-nl2br-function-inside-while-and-echo/#findComment-1585593 Share on other sites More sharing options...
Fishcakes Posted April 4, 2021 Author Share Posted April 4, 2021 Ace that did it thanks Quote Link to comment https://forums.phpfreaks.com/topic/312422-using-nl2br-function-inside-while-and-echo/#findComment-1585594 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.