PHPBuilder - Strings & Text in PHP - The ABCs of PHP Part 5 Page 3



RSS Twitter
Articles Php Gtk

Strings & Text in PHP - The ABCs of PHP Part 5 - Page 3

by: PHP Builder Staff
|
April 15, 2009

Some of the purists out there will also have you believe that using "" is significantly slower than using '' and yes in a way, there are grounds for this argument because of the extra work that has to be done for strings using "", in reality however I've tested this theory a number of times, and found that it's dependent on the speed of the server and/or PC your running on, and in most cases the difference is so small that it's not really worth the argument. At the end of the day however, again it is a personal decision as to what method you use.
So what about these special escape sequences you mentioned?
Escape sequences are one or more characters in sequence preceded by a \ symbol, usually there is only one character following the \ the most common of which are as follows:
  • \n Linefeed
  • \r Carriage return
  • \t Tab
  • \\ An actual \ symbol
  • \" Double quotes
  • \' Single quotes
In most cases when using a line feed, you can just use \n under Windows or Linux/Unix, however if you are writing files on the 2 platforms you may need to watch the differences.
Under windows, a normal end of line is actually \r\n, where as under linux it just a \n
What problem does this cause? Well if your creating a text file, then using just a \n under Windows will mean your file will end up like this:
Line1
Line2
Under Linux/Unix your text file will end up like this
Line1
Line2
If however you use \r\n, under Windows your file will look like this:
Line1
Line2
Under Linux/Unix however, the output is undefined. This means that sometimes it may look right, other times it may not, but there is no "same way" every time.
\t is usually used to line up columns of text at the console, and just like using the tab key in a text editor such as notepad, will attempt to line up the columns as long as the text fits within it's width. EG:
<?php

  Print "Hello\tworld\n";
  Print "Hello\tworld\n";

?>
Will produce the following output:
Hello   World
Hello   World
However, if the text in the first column is longer
<?php

  Print "Hello Hello Hello\tworld\n";
  Print "Hello\tworld\n";

?>
The following will be seen:
Hello Hello Hello  World
Hello   World

« Previous Page
1
|
2
|
3
|
4
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:

Comment:



Comment:

(Maximum characters: 1200). You have characters left.