Dragen Posted May 31, 2007 Share Posted May 31, 2007 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. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 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. Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 31, 2007 Author Share Posted May 31, 2007 so there isn't an actual method for doing it without mucking about with other functions? okay, thanks Quote Link to comment Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 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'; Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 31, 2007 Share Posted May 31, 2007 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'; ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 31, 2007 Share Posted May 31, 2007 If you don't want to use double quotes then you will have to format the string. eg: $str = 'A new line A tab'; echo '<pre>' . $str . '</pre>'; Why do you not want to use double quotes? Quote Link to comment Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 You can't win, you can't lose, you can't break even... you can't even get out of the game. It seems to do live by that motto huh? lol Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 31, 2007 Author Share Posted May 31, 2007 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> Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 31, 2007 Share Posted May 31, 2007 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; Quote Link to comment Share on other sites More sharing options...
corbin Posted May 31, 2007 Share Posted May 31, 2007 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!'; Quote Link to comment Share on other sites More sharing options...
saf Posted May 31, 2007 Share Posted May 31, 2007 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. Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 31, 2007 Share Posted May 31, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.