A little note about the new line character, Ajax and a silly mistake…

Just something “fun” to share on Friday…

I had a simple Ajax auto-complete type of widget in my app… which ultimately returned this view:

if(!empty($items)) {
     foreach($items as $key => $item) {
       echo $item . '\n';

Everything worked well, except that \n was not treated as a new line character. So, all items were returned as one long string, rather than being broken down into “individual” lines in the auto-complete widget.

After some wall and head connections, I’ve found out that instead of ‘\n’ I have to use double quotes “\n” to get this to work as expected. Problem solved.

Hopefully it’ll help someone out there :)

  • rafaelbandeira3

    notice that nothing is parsed or processed in single quoted strings, not just escaped characters. That’s why using single quotes when possible will speed up your app.

  • @rafaelbandeira3

    Yep, that’s a very good point. Too bad I’m not as quick on that on Fridays, as I would like to be :)

    Side note… I’ve been testing your ‘linkable’ behavior slowly, but surely and so far, nothing, but smiles.
    Good stuff. I’ll probably do a little post about it in the near future. A lot of people have been waiting for this ;)

  • I once ran into the same problem when I was newer to PHP, took about 20 minutes to figure it out. Never made the same problem since though, and you probably won’t either ;)

  • @Jonah Turnquist

    I’m not sure :)… considering I’ve been using PHP for about 10 years, and still “forget” things like that.
    But let’s hope so …

  • Marc

    It happens to me all the time. But I always notice it right away because my syntax highlighter displays parsed symbols like \n in another color and I start wondering when it doesn’t

  • @Marc

    That’s quite nice, wish I had that feature a few hours ago :)

  • Silver Knight

    @Marc, teknoid:

    I’d be utterly lost half the time without my syntax highlighter, often for the same reason as Marc states. I frequently use kate (KDE “Advanced” Text Editor or somesuch) or Quanta+ under Linux, but regardless of OS, EVERY coder new or old will likely find that they enjoy using an editor with at least the OPTION of decent syntax highlighting for any code they must stare at for hours. It’s just far too useful and readily available to NOT have it.

    Notepad++ (http://tinyurl.com/notepadpp-portable) comes to mind as one free and easy to get option for Windows users, and TextWrangler (http://tinyurl.com/textwrangler) a similar option for Mac users. I’m sure there’s dozens of other choices for every OS, but those are the ones I can think of off the top of my head. :)

  • @Silver Knight

    I use Aptana as my IDE, because it has quite a few amazing features, but unfortunately the default highlighting doesn’t show escaped vs. un-escaped strings in different color, I’m sure there is a way to tweak that, but I’m way too lazy to dig around the preferences :)

    p.s. If anyone knows how to enable that feature for Eclipse/Aptana, please share…

  • Silver Knight


    I’ve played with Aptana on occasion and while I’m rather impressed with it’s “quite a few amazing features” (and it IS a very nice and powerful IDE with an excellent feature set) I always end up back in Quanta+ for my web development tasks due to it’s simplicity and it’s use of the kate editor part as it’s editor (which has some of the best syntax highlighting I’ve ever enjoyed).

    This exchange has gotten me curious now… What other syntax highlighting editors/IDE environments are in somewhat frequent use out there by web developers? I know, of course, about the more common ones (Dreamweaver, Aptana / other Eclipse-based options, Quanta+, etc.) and the various simpler editors (Notepad++, BBEdit, etc.) but are there any other really excellent alternatives in somewhat common use that I’ve been missing out on just because I don’t know they exist?

    As to enabling different syntax highlighting for escaped vs. un-escaped strings in Aptana, I’m not 100% certain, but I believe that comes down to the syntax highlighting definition itself. Pretty sure that has to be defined explicitly in the syntax highlight file and would require a bit of twiddling to get it doing what you’d want. I’ll have a closer look at it next time I open up Aptana on my own system and if I can discover what you want I’ll happily post it here for you.


  • Chikiro

    I use PHP_EOL for such things.

  • @Chikiro

    Excellent, thanks for sharing…

    @Silver Knight

    I’ve tried a lot of popular IDE’s… in addition to the ones mentioned: Komodo, Zend (which is really just about the same as Eclipse, at least as far as CakePHP and my development is concerned), PhpED, PHP Designer and maybe some others.

    The reasoning why I stuck with Aptana is kind of like this:

    – It has some lovely features for jQuery (and other JS frameworks) and CSS, which are not found in other IDE’s.
    – It’s free… when you can get such a high-quality IDE as Eclipse/Aptana for free, there should be no reason to spend money on some other one.
    – I don’t know if you saw my recent post about their “Cloud” service, but to me it opens up some very unique opportunities (not to mention convenience) and so far has been a pleasure to work with.
    – Seems like the people behind Aptana are on top of web-dev scene and are always listening to the community.
    – There is a near-term potential for an environment that would be specifically tailored to CakePHP app development and deployment. (I’m actually brainstorming on the addition of CakePHP plug-in to the IDE). You can take a look at their RoR support… to have something similar (well… better) for CakePHP would be killer.
    – Combine that with seamless Cloud deployment and I see some very interesting things on the horizon ;)

  • Silver Knight

    I did indeed see your recent post about their cloud service, and to be honest, that post may be the reason I have another look at Aptana very soon. I’m also a huge fan of both jQuery and CSS and become more-so each day as I become more familiar and comfortable with them, and so I’ll be looking for those jQuery and CSS features in Aptana the next time I open it. Thanks for your rundown on what makes Aptana the IDE for you. It will give me a good reason to take a closer look at it, and gives me a better idea of what to look for in it.

    As to adding CakePHP integration to Aptana/Eclipse, I’m all for that idea 100%. Especially if it’s got some decent integration support for using CakePHP with jQuery, MooTools, ExtJS, etc, even more easily than it already is. (For a coder it’s pretty dead simple to use any of these things with CakePHP, but it never hurts to have handy timesaving helpers in your IDE.) In my case, I’ve just been using the “Code Snippets” functionality in Quanta+ as a timesaver when working with tying CakePHP together with external libraries, but if the IDE had more “direct” support, that’d be nice too. ;)

  • @Silver Knight

    Sounds good. Hopefully these things I mentioned shall be coming up soon enough, too bad there are only 24 hours in one day :)

  • Sun’s new version of NetBeans is supposed to have good PHP support (and CakePHP too?). I use Crimson Editor as my simple syntax highlighting editor, and Intype (beta) as an alternative – can’t wait for Intype developers to release a more finished version…HTML creation is awesome, PHP is lacking (with regard to macros, TextMate style) – which I could fix/update myself, but am not looking forward to that until they release new version with new engine.

    For an IDE I have Zend’s version of Eclipse in Zend Studio v6.x. To be honest, I don’t really like it, but it’s the only editor that I have which gives me some support for debugging and project introspection.

  • @Brendon Kozlowski

    Thanks for your comment.
    I really think you should check out Aptana (if you’ve used Eclipse and Zend), it beats them, imo. Besides the facts stated before based on being less bloated, very highly configurable with plugins and having some really nice synchronization features (including their cloud), I’m beginning to enjoy it more and more daily :) (Still have a lot learn).
    But to me the IDE has to be a powerful, but productive tool, like the framework itself, which… of course… takes time to master.

  • I intend to take a look at both Aptana, and the new Netbeans. Sitepoint has a new writeup on it. If you haven’t completely decided to go with Aptana, you may want to at least take a read on it:

    Thanks again for all your tips and wisdom. :)

  • Nevermind, I’m going with Aptana. It looks cleaner, and supports Adobe Air, jQuery, and iPhone development as well – hard to beat that.

  • @Brendon Kozlowski

    No problem ;)

  • brian

    You can simplify that a little with:

    if(!empty($items)) {
    echo implode(“\n”, array_values($items));

    It’ll also not leave your data with a trailing newline.

    (I was working on my own autocomplete component tonight ;-)

  • @brian

    Nice, thank you for sharing.

  • Hey thanks, was googling for answers on this, i actually had ‘”/n”‘ didnt notice until i seen your post

  • @Adamzski

    Cool, glad it came in handy.

%d bloggers like this: