Suscriber with us



Receive HTML?

Syndicate

PHP String PDF Print E-mail

A string variable is used to store and manipulate a piece of text.

Strings in PHP

String variables are used for values that contains character strings.

In this tutorial we are going to look at some of the most common functions and operators used to manipulate strings in PHP.

String Creation

Before you can use a string you have to create it! A string can be used directly in a function or it can be stored in a variable. Below we create the exact same string twice: first storing it into a variable and in the second case we place the string directly into a function.

After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.

Below, the PHP script assigns the string "Hello World" to a string variable called $txt:

<?php 
$t="Hello World"; 
echo $t; 
?>

The output of the code above will be:

Hello World

String Creation Single Quotes

Thus far we have created strings using double-quotes, but it is just as correct to create a string using single-quotes, otherwise known as apostrophes.

PHP Code:

$string = 'Unlock your potential!'; 
echo 'Unlock your potential!'; 
echo $string;

If you want to use a single-quote within the string you have to escape the single-quote with a backslash \ . Like this: \' !

PHP Code: 

echo ' It\'s Neat!';

String Creation Double-Quotes

We have used double-quotes and will continue to use them as the primary method for forming strings. Double-quotes allow for many special escaped characters to be used that you cannot do with a single-quote string. Once again, a backslash is used to escape a character.

PHP Code:

$nline = "A newline is \n"; 
$return = "A carriage return is \r"; 
$tab = "A tab is \t"; 
$dollar = "A dollar sign is \$"; 
$doublequote = "A double-quote is \"";

Note: If you try to escape a character that doesn't need to be, such as an apostrophe, then the backslash will show up when you output the string.

These escaped characters are not very useful for outputting to a web page because HTML ignore extra white space. A tab, newline, and carriage return are all examples of extra (ignorable) white space. However, when writing to a file that may be read by human eyes these escaped characters are a valuable tool!

PHP - String Creation Heredoc

The two methods above are the conventional way to create strings in most programming languages. PHP introduces a more robust string creation tool called heredoc that lets the programmer create multi-line strings without using quotations. However, creating a string using heredoc is more difficult and can lead to problems if you do not correctly code your string! Here's how to do it:

PHP Code:

$string = <<<TEST 
Unlock your potential! 
TEST; 
echo $string;

There are a few very important things to remember when using heredoc.

  • Use <<< and some identifier that you choose to begin the heredoc. In this example we chose TEST as our identifier.
  • Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was TEST;
  • The closing sequence TEST; must occur on a line by itself and cannot be indented!

Another thing to note is that when you output this multi-line string to a web page, it will not span multiple lines because we did not have any <br /> tags contained inside our string! Here is the output made from the code above.                                      



Last Updated ( Thursday, 27 March 2008 )
 
< Prev   Next >

Polls

Which is the best Scripting language?