Main Content

Setting up strings in PHP

Archive - Originally posted on "The Horse's Mouth" - 2013-04-27 20:25:19 - Graham Ellis

Inside, the web is all about passing around and manipulating strings of text - so the programming langauges of the web such as PHP have to be really strong in their string handling facilities. I give you two new examples from last week's course ... easy lessons that form the bedrock on which the powerful stuff is built:

An (almost) literal string - what you see is what you get:
  $literal = 'This is a string with $things and \t things in it!';

A string with variable interpollation and \ escaped special sequences:
  $interp = "This is a string with $things and \t things in it!";

A string which is run as an operating system command, and the result is returned to you:
  $ranit = `uptime`;

As well as using the single characters shown above, PHP supports a "here document format where the delimiter is any unique word / set of characters that will appear to terminate on a line of its own.
  $terms = <<< LONG
  This is available subject to the laws of Papua New Guinea as they
  were in 1847, together with royal approval from King Zog of
  Albania when eating $things.
  LONG;


See how that runs [here]. Full source code [here]