Jump to content
justinh

new line \n not working

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

Share this post


Link to post
Share on other sites

If you view source you will find it stacked, but html will force it into one line, that is why you require html line break <br> to force line break.n to line break outputted code from server<br> line break of html code

Share this post


Link to post
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...)

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...