Jump to content

new line \n not working


justinh

Recommended Posts

I'm using UniServer Zero XI with PHP Version 5.4.42 (on Win7). I've tried different ways to force a new line in the web page, like with n. Only HTML BR tag seems to work.

 

Here is an exmple:

<!DOCTYPE html><html><body><?phpfunction makecoffee($type = "cappuccino"){    return "Making a cup of $type.n";}echo makecoffee();echo makecoffee(null);echo makecoffee("espresso");?></body><html>

Output is:

Making a cup of cappuccino. Making a cup of . Making a cup of espresso.

 

But should be:

  Making a cup of cappuccino.  Making a cup of .  Making a cup of espresso.

Is this a server configuration issue?

 

Appreciate the input,

Justin

Link to comment
Share on other sites

Great info, dsonesuk! Now, can you give some details to help me understand better. Correct, the text is stacked in source.

However, where else can I render PHP output besides a web page? I guess a file or a dialog box?

 

So, is the bottom line that, if I am outputting to a web page, no need to bother with n, just use <br>?

 

I guess I'm in rebellion a bit, because I want to use 'php' code in my PHP code and not HTML in my PHP code :Bucktooth:

 

Thanks much! (yes, I'm a new newbie...)

Link to comment
Share on other sites

At some stage you will have to use html in php, and php allows this there is no way to escape this, and you are not limited to br, wrapping within <p> or any block element tag will give you the same result. Sometimes i would use n on html tags to make the htlm code easy to read, instead in some cases where the code is several continuous lines of html code in one block.

Link to comment
Share on other sites

You can wrap the output in <pre></pre> tags. But this too is HTML (you're sending data to a web browser so you may not always escape it) and it may cause the output to display in monospace fonts.Also, the nl2br() function can be used; you can also use heredoc to format your text so you won't need n.

Link to comment
Share on other sites

If you want to practice PHP without running it with a web server then you can just run it on the command line. The command line environment is different than a web server environment, things that you use with a server like $_SESSION, $_GET, $_POST, etc are not defined on a command line. But, it's a quick and easy way to run a piece of code to test. You use the -f flag to tell php.exe that you want it to execute a particular file, e.g.:php -f c:pathtofile.phphttp://php.net/manual/en/features.commandline.phpIf you run it on a command line, then outputting a newline character will show as a new line because it's just text output. It will still output the newline when sending output to the browser, but the browser ignores newlines when it expects HTML.Another alternative might be to send a content-type header to tell the browser that you are sending text instead of HTML, so the browser will not try to render it as HTML. By default, if you don't send a content-type header then PHP will send text/html. You can sent text/plain to override that.header('Content-type: text/plain');Note that headers don't make sense either when using the command line, only with a web server.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...