PHP newline

I’m trying to learn PHP from a book (PHP, A Beginner’s Guide, by Vikram Vaswani) and I’ve run into problems right off the bat. The first chapter’s exercises includes a statement <?php echo "Today looks \nbright and sunny" ?> which is supposed to produce two lines of simple text, broken at the \n newline character, however, it’s not. Everything appears on one line. Did I miss something somewhere that I’m not implementing the newline correctly? I’m learning on a Win XP machine and using PHP 5.2.9-2 in a Firefox 52.5.3 browser.

What am I missing?

TIA

which is supposed to produce two lines of simple text

Not when output on a web page and viewed with a browser. New-line characters don’t have any direct meaning in html content. They format the source code. The
tag causes a new line in html content. You can also enclose the output inside

 
tags to cause the new-line character to be rendered as is. You can also use php’s nl2br() function to add a
at every new-line character.

You don’t even have to do it in php, for you could do it in good old CSS:

white-space: pre-line;

Or run it in a console/terminal/command line

php yourfile.php

The

 and 
tags worked great.

I’m supposed to be able to do these exercises as written, but I’m wondering if the book is too old for all the changes that have occurred since 2009. I like the format of the book so I’ll be looking for an updated edition.

Thanks to all!

I think its just a misunderstanding, the copy i downloaded uses proper prepared and parameterized queries so it doesnt seem that bad

if you view source in your browser you will see the newlines, its just that the parsed html page will not show them as is not html/css

Php 5.2 ??

For gawd sake, use a current version of Php. That is hundreds and hundreds of versions behind. If the book you are using says to use it, then throw the book away.

Install XAMPP. Read the FAQ on their site if you have an issue with XP https://www.apachefriends.org/index.html

The book refers to PHP 5.3 as the current version. It’s 9 years old (the book)! Sorry OP - I completely missed this

As many current tutorials fail to use proper db queries it’s impressive this book does. But a lot has happened in 9 years so I’d have to agree - find a newer book. Though if they tell you to escape data before doing db queries then throw that away as well.

The book should also cover unit testing, if not I’d question the quality of the rest of it

Sponsor our Newsletter | Privacy Policy | Terms of Service