Jump to content

using \t and \n with echo single quotes..


Dragen

Recommended Posts

okay.. this question is going to sound highly stupendous, but...

 

If I use thise code:

echo "hi\nmy name is\tlee";

That will print out:

hi

my name is lee

 

but if I surround it in single quotes it prints out the \n and \t.. so this:

echo 'hi\nmy name is\tlee';

ountputs:

hi\nmy name is\tlee

How can I print tabs and line spaces into the html code when using single quotes? It's mainly for looks when I go to view source, so the syntax is arranged better.

Link to comment
Share on other sites

Single quotes don't recognize those characters as special.  You could use concatenation or chr(), but that would look uglier than double quotes.  You could use:

 

sprintf('%sThis line is indented.%sThis line is not.',"\t",chr(13));

echo 'Or something like this' . "\n";

 

And I think heredocs wouldn't be your cup of tea either.

Link to comment
Share on other sites

Nope there is not. However you may be able to get by with something like this, unsure:

 

$n = "\n";
$t = "\t";

echo 'hi' . $n . 'my name is' . $t . 'lee';

 

Hehe... might as well do:

<?php
echo 'hi' . "\n" . 'my name is' . "\t" . 'lee';
?>

 

:D

Link to comment
Share on other sites

I want to use single quotes instead of double to stop having to use \" every time I want to output any html, such as:

<a href=\"mylink.php\">click here</a>

 

If you're assigning a large code block, just use HEREDOC syntax instead:

$string = <<<EOS
<a href="mylink.php">click here</a>
EOS;

Link to comment
Share on other sites

I use single quotes all the time unless I'm messing with out putting a lot of variables, and I just usually go with:

 

$str = 'this is some <font color="red">text</font>' . "\r\n" . 'this is some more text on a separate line!';

Link to comment
Share on other sites

You could always escape out of PHP, add some HTML, and come back into PHP.

 

But if you are also concerened about the speed of the script, then single quotes are known to help speed up execution time. Plus if you are extremely concerned with speed, you will not care about what "view source" generates.

Link to comment
Share on other sites

But if you are also concerened about the speed of the script, then single quotes are known to help speed up execution time.

 

Actually, you can read some of the benchmark testing that was done both on these forums and elsewhere to tell that the difference between double and single quotes when concerned with standard strings is negligible.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.