noob - Escape sequences - \n, \r not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
matt0ne
Forum Newbie
Posts: 3
Joined: Tue Jan 02, 2007 8:53 pm
Location: Madison, WI

noob - Escape sequences - \n, \r not working

Post by matt0ne »

I am trying to teach myself PHP from O'Reilly's Understanding PHP book. I have followed early examples which have worked fine except that the escape sequences in double-quoted strings aren't working properly.

I am using MAMP (php 5.1.6), writing code with textmate, and testing with both Safari & Firefox (os x).

I'm hoping that this is something really simple - i can't find any documentation on this problem anywhere.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Show some code and tell me what you expect to be seeing.
From what you have said I can't see any fault on your part.
woger
Forum Newbie
Posts: 13
Joined: Tue Jan 02, 2007 4:03 am

Post by woger »

The escape sequences don't necessarily appear on your screen. They are usually only seen when you view the source code - just to make the code more readable.

It doesn't mean you'll get a new line on your screen.

Hope that helps -
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Without seeing any code or knowing the precise situation this is difficult to answer.
If you are wanting to see a blank line in HTML you need to use "<br />" not "\n". As has already been stated the \n only shows up when you view the source code.

The other problem some people have is they include \n in single quote block strings which do not work.
User avatar
matt0ne
Forum Newbie
Posts: 3
Joined: Tue Jan 02, 2007 8:53 pm
Location: Madison, WI

Code: From O'reilly book

Post by matt0ne »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is the code that I am trying:

Code: Select all

$person[0] = "Edison";
$person[1] = "Wankel";
$person[2] = "Crapper";

$creator['Light bulb'] = "Edison";
$creator['Rotary Engine'] = "Wankel";
$creator['Toilet'] = "Crapper";
The array( ) construct creates an array:

$person = array('Edison', 'Wankel', 'Crapper');
$creator = array('Light bulb'    => 'Edison',
                 'Rotary Engine' => 'Wankel',
                 'Toilet'        => 'Crapper');


foreach ($person as $name) {
  echo "Hello, $name\n";
}
foreach ($creator as $invention => $inventor) {
  echo "$inventor created the $invention\n";
}
I would expect this to display:
Hello, Edison
Hello, Wankel
Hello, Crapper
Edison created the Light bulb
Wankel created the Rotary Engine
Crapper created the Toilet
instead I get this:
Hello, Edison Hello, Wankel Hello, Crapper Edison created the Light bulb Wankel created the Rotary Engine Crapper created the Toilet

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

If you are looking at through your browser that is the way it should display. Open the page's source and you should see it display with breaks as you specified. BTW in order to use "\r\n" you need to you inch marks.
User avatar
matt0ne
Forum Newbie
Posts: 3
Joined: Tue Jan 02, 2007 8:53 pm
Location: Madison, WI

Post by matt0ne »

OK thanks, that clears that up. It was driving me crazy last night!
Post Reply