More Fun with PHP Strings

Posted: February 27, 2009

PHP is a pretty good language to work with text.  Some programming languages have separate types for single characters (chars) and strings.  With PHP it’s pretty much all the same.  Some languages (I’m looking at you C!) don’t allow strings at all, and only have characters.  So, with as much as PHP keeps out of your way, strings aren’t that bad.

Last time we talked a little bit about dealing with text.  We went over single versus double quotes, and outputting text to the browser with echo.  What if you want to do other things with your text, like add to it for example?

You can concatenate, or add strings together with a period.

$foo = ‘some text’;
$fee = ‘ blah blah’;
echo $foo.$fee;

gives us…

some text blah blah

You can concatenate variables, like in the above example.  Or you can include strings of text.

echo $foo.‘ text in the middle ’.$fee;

One thing to remember is to include spaces in your strings you assign to a variable.  If you look at the above foo/fee examples, I put spaces before or after some of the strings so that it would be outputted with a space between the two strings.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • TwitThis
  • Yahoo! Buzz
  • Propeller

Leave a Reply