Launching Soon
PHP Intro
PHP Syntax
PHP Variables
PHP Echo
PHP String
PHP Operators
PHP If...Else
PHP Switch
PHP Arrays
PHP While Loops
PHP For Loops
PHP Functions
PHP Forms
PHP Advanced
PHP Date
PHP Include
PHP File
PHP File Create
PHP File Open
PHP File Close
PHP File Write
PHP File Read
PHP File Delete
PHP File Append
PHP File Truncate
PHP File Upload
PHP Cookies
PHP Sesssions
PHP Email
PHP Secure Email
PHP Error
PHP Exception
PHP Filter
PHP Database
MY SQL Introduction
MY SQL Connect
MY SQL Create
MY SQL Insert
MY SQL Where
MY SQLOrder By
MY SQL Update
MY SQL Delete
PHP Array
PHP Calender
PHP Date
PHP Directory
PHP Error
PHP File System
PHP Filter
PHP Libxml
PHP Mail
PHP Misc

PHP Mysql

PHP String


php - strings

In the last lesson, PHP Echo, we used strings a bit, but didn't talk about them in depth. Throughout your PHP career you will be using strings a great deal, so it is important to have a basic understanding of PHP strings.



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 send the string directly to echo.

PHP Code:


$my_string = "MGEF - Unlock your potential!";  
echo "MGEF - Unlock your potential!";  
echo $my_string;


In the above example the first string will be stored into the variable $my_string, while the second string will be used in the echo and not be stored. Remember to save your strings into variables if you plan on using them more than once! Below is the output from our example code. They look identical just as we thought.



MGEF - Unlock your potential! MGEF - Unlock your potential!  


php - 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:

$my_string = 'MGEF - Unlock your potential!';  
echo 'MGEF - Unlock your potential!';  
echo $my_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 'MGEF - It\'s Neat!';


php - 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:

$newline = "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 traditional way to create strings in most programming languages. PHP introduces a more robust string creation tool calledheredoc 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 properly code your string! Here's how to do it:

PHP Code:


$my_string = <<<TEST  
Webmaster Tutorials  
Unlock your potential!  
echo $my_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.

Display: Webmaster Tutorials Unlock your potential!  

Once again, take great care in following the heredoc creation guidelines to avoid any headaches.


Designed and Developed by MGEF
PHP MGEF Tutorial is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2010 by MGEF. All Rights Reserved.