Monday, March 24, 2008

PHP Coding Tips - Strings

1) As a finesse thing, I use single quotes around strings whenever possible (e.g. strings that don't contain variables, single quotes, \n, etc.). This is supposed to make less work for the PHP parser.2) When an array variable isn't in a string, put quotes around string-literal keys so they are not regarded as constants:
PHP Code:
// OK
echo $row[$key];
// Wrong, unless key is a constant
echo $row[key];
// Right
echo $row['key'];
// OK, since it's in a string
echo "Text: $row[key]";
3) Remember, you can break out of PHP mode for large sections of HTML. This is faster than echo'ing and you don't need to escape quotes.
-------------------------------------------------------------------------------------------------
Personally I avoid any code that looks like this:
PHP Code:
$something = 'this';
$something .= 'and this';
$something .= 'and this';
$something .= 'and this';
$something .= 'and this';
or this:
PHP Code:
$something = 'this'
. 'and this'
. 'and this'
. 'and this'
. 'and this'
. 'and this'
. 'and this';
I much prefer the following:
PHP Code:
$something = 'this
and this
and this
and this';
Why? Because in the first two examples PHP is having to allocate memory for multiple strings and then go through the (relatively) expensive process of "sticking" them together again. If you just declare a string over multiple lines you are avoiding that overhead. You have to be careful to make code readable if you do this but I've never had any problems with it.Here's a handy (relatively undocumented) tip. PHP supports the following method of assigning strings (borrowed from Perl):
PHP Code:
$string = <<This is a string
It can include both 'single' and "double" quotes
without needing to escape them. However, $variables
will still be interpolated as they are in double
quoted strings. Complex variable expressions such as
{$array['element']} or {$object->property} can also
be included and will be evaluated if they are included
in curly braces (they may work without curly braces
but I tend to include them for added clarity). The
string will terminate with whatever you specified
at the start like this:
ENDOFSTRING;
>>>>See php.net for more info.<<<<
-------------------------------------------------------------------------------------------------
You can eek a bit more speed out of your code, especially if you have LOTS of strings, by using single quotes and concanetating variables in (I can't spell today...sigh)
PHP Code:
$string = 'this is a string with '.$foo.' in it';
// is marginally faster than
$string = "this is a string with $foo in it";
The reason is php has to search through the string to find the variable.
-------------------------------------------------------------------------------------------------
It's better to use a comma instead of a dot, because it saves some overhead (no string concatenation).
PHP Code:
// Example:
echo 'foo', 'bar';
// translates to:
echo 'foo';
echo 'bar';
// whereas:
echo 'foo' . 'bar'
// translates to:
echo 'foobar'
The first is cheaper, because PHP doesn't first have to create the new string 'foobar', but can instead send the two strings to output directly.
-------------------------------------------------------------------------------------------------
to echo big chunks of html with vars in the middle using single quotes, instead of double quotes (slower) or jumping in and out of php, even if using shorthand, I do it like this
PHP Code:
echo '
Hello,

My name is ',$name,' ',$lastname,' and am ',$age,' years old.

I live in ',$city,', ',$country,' since I was born.

You can contact me at ',$email,' or by phone at ',$phone,'

Regards,

',$name,' ',$lastname,'

';
it's very shorthand and fast... it can be very comfortable to use in a simple php template system
-------------------------------------------------------------------------------------------------
Consider using str_replace instead of the preg_replace or ereg_replace functions. The only reason why you would use preg_replace or ereg_replace functions would be that you REALLY need to use regular expressions. Also, as of PHP 4.0.5, every parameter in str_replace() can be an array, so theres no excuse to use preg or ereg fucntions when replacing simpel strings anymore.A useful use of str_replace() :
PHP Code:
$string="The quick brown fox jumps over the lazy dog.";
$patterns[0] = "quick";
$patterns[1] = "brown";
$patterns[2] = "fox";
$replacements[0] = "slow";
$replacements[1] = "black";
$replacements[2] = "bear";
$string=str_replace($patterns, $replacements, $string);
//$string="The slow black bear jumps over the lazy dog."
-------------------------------------------------------------------------------------------------
ereg vs pregWhen it comes to the regular expression functions, ereg* and preg*, the preg functions are the clear choice. The preg functions are generally twice as fast as their ereg counterpart. They also support more advanced regular expression operations. I can't think of any reason why you would need to use the ereg functions.preg manual page and pattern syntax (long and confusing but pretty good).
-------------------------------------------------------------------------------------------------
Here's a bit of code I use to change new lines into XHTML

's and
's.
PHP Code:
// the first bit is to make both "\r\n" and "\r" line endings into "\n"
$string = str_replace("\r\n", "\n", $string);
$string = str_replace("\r", "\n", $string);
// this next bit first adds an opening

then makes all double line breaks into


$string = "

".str_replace("\n\n", "

", $string);
// finally this turns any single line breaks into
's and closes the last


$string = str_replace("\n", "
", $string)."

";
-------------------------------------------------------------------------------------------------

No comments: