Php "\n"

software development

#1

I’m new to php and keep seeing a “\n” in examples of code. I’m not familar with this. Can somebody explain what this is and how it is used. I am currently reviewing “OReilly’s” Web Database applications with PHP & MySQL and see if referenced here alot. ie simple statement
echo “


\n”;

#2

I don’t know PHP well, but I’d assume that “\n” is a newline (ie a carriage return).


#3

Hi ucurl -

The “\n”, as Will mentioned, is a carriage return. In other words, this:

print “Foo\n\n”;

…would print out the same thing as if you typed the word “Foo” and hit return twice.

  • Jeff @ DreamHost
  • DH Discussion Forum Admin

#4

Thanks guys. I thought it was more. I guess the examples then relate to how the resulting HTML will print out …once echo is executed.

Much appreciated.
u


#5

[quote]The “\n”, as Will mentioned, is a carriage return.

[/quote]

You been using a Mac or sumpin, Jeff?

It’s my understanding that on a Unix/Linux system a newline consists of a linefeed (0x0a).

On a DOS/Windows system, a newline generates two characters, a carriage return (0x0d) followed by a linefeed (0x0a).


#6

[quote]You been using a Mac or sumpin, Jeff?

[/quote]

Yup. :>

  • Jeff @ DreamHost
  • DH Discussion Forum Admin

#7

While we’re on the topic of PHP, what the heck does the “s” in “%s” mean? For example:

printf("<a href="$PHP_SELF?filter=%s">%s |
",$catrow[“categoryID”],$catrow[“categoryName”]);

This is a code snippet from my own code, so I understand what it DOES and am USING it just fine. I’m just curious if the “s” has any meaning. Is it short for something?

Thanks,
Gabe


#8

This is actually part of printf and not of PHP. I believe %s stands for string; it’s used a lot in C. I may well be wrong about this though.

man sprintf.


#9

Will’s right. In printf() (note, printf() is special. this doesn’t work in print() or echo()) %s means string, %f means float, %d means decimal, etc etc.

Works in perl too. It’s a C thing. Most non-C-heads don’t use it. I do occasionally but we usually don’t use it around here. It makes complex statements less messy.

And thanks for the linefeed message, deke. I caught myself mentally saying “Well, ACTUALLY…” but then you said it for me.

nate.


#10

Good question on th %s, i’m using it as well.