Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Multiline Strings
#5
(11-11-2009, 04:45 PM)Spl3en Wrote: Thanks for this tips, it's really useful for making a code easy to read.
Another manner to do that, a bit more "proper" is to use chr(10) IMHO.
Example :
PHP Code:
<?php
$multiline 
chr(10);
echo 
"Hello".$multiline."This is multilines!";
?>

How is this more proper? the chr() function returns the ascii value of a number, so how does calling a function to get the ascii value of 10 (I'm guessing it's the newline char, as that's the only thing that would make sense in this case) a more proper way of implementing a multiline string?

What you did makes use of a needless variable, and it is harder to understand what you were trying to accomplish.

Another possible way of implementing newline's is '\n', which would look something like:

PHP Code:
echo "lol \n new line"

However, \n's aren't really an alternative to heredoc's as they aren't really friendly to your eyes.

Think about it, which one makes more sense?

PHP Code:
$intro "Hello, sir!\ntoday we will be offering you lots of stuff.\nyou can get a tv\na computer\nor a new desk!\nchoose now.\n"

or...

PHP Code:
$intro = <<<EOL
Hello, sir!
today we will be offering you lots of stuff.
you can get a tv
a computer
or a new desk!
choose now.
EOL; 

Sorry, I kinda rambled there. In short, chr() should definitely not be used for implementing newlines within a string ;).

Also, nice guide Master Of The Universe.
Reply


Messages In This Thread
PHP Multiline Strings - by Gaijin - 11-10-2009, 02:35 AM
RE: PHP Multiline Strings - by zone - 11-10-2009, 03:17 AM
RE: PHP Multiline Strings - by Gaijin - 11-10-2009, 11:36 AM
RE: PHP Multiline Strings - by Spl3en - 11-11-2009, 04:45 PM
RE: PHP Multiline Strings - by wat - 11-17-2009, 08:17 PM
RE: PHP Multiline Strings - by Gaijin - 11-17-2009, 08:30 PM
RE: PHP Multiline Strings - by wat - 11-17-2009, 08:34 PM
RE: PHP Multiline Strings - by Gaijin - 11-17-2009, 08:40 PM
RE: PHP Multiline Strings - by Omniscient - 11-17-2009, 09:39 PM
RE: PHP Multiline Strings - by Gaijin - 11-17-2009, 09:50 PM
RE: PHP Multiline Strings - by Spl3en - 11-18-2009, 03:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 816 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,344 10-10-2011, 01:00 PM
Last Post: Greyersting

Forum Jump:


Users browsing this thread: 1 Guest(s)