Page 1 of 1
noob - Escape sequences - \n, \r not working
Posted: Tue Jan 02, 2007 9:02 pm
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.
Posted: Tue Jan 02, 2007 9:11 pm
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.
Posted: Tue Jan 02, 2007 11:20 pm
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 -
Posted: Wed Jan 03, 2007 4:28 am
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.
Code: From O'reilly book
Posted: Wed Jan 03, 2007 1:51 pm
by matt0ne
feyd | Please use 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
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]
Posted: Wed Jan 03, 2007 1:53 pm
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.
Posted: Wed Jan 03, 2007 1:55 pm
by matt0ne
OK thanks, that clears that up. It was driving me crazy last night!