#native_company# #native_desc#
#native_cta#

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

By PHP Builder Staff
on 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 rn, 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 rn, 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 "Hellotworldn";
  Print "Hellotworldn";

?>
Will produce the following output:

Hello   World
Hello   World
However, if the text in the first column is longer
<?php

  Print "Hello Hello Hellotworldn";
  Print "Hellotworldn";

?>
The following will be seen:

Hello Hello Hello  World
Hello   World