Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Output and strings

  1. Output of strings
  2. Print html code
  3. Special characters in strings

1. Output of strings

The main goal of php scripts, beside any programm logic behinds it, is to generate an output, e.g. a list of news. You use the language construct echo to print some text without leaving the php mode. Check the following example.

<?php
echo 'A text which is printed';
?>

You notice first the parentheses of the function are missing. But thats not an error as echo is not a function but a language construct. The manual explains this as follow:

echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it.

The second thing you noticed is the text to print is written in two apostrophes '. This is the way (independent of echo) to create a string with the written content. This string is the parameter for the language construct echo and it uses it to print the string. If you upload this script to your webserver and runs it you get the following output.

A text which is printed

Now we add a second echo statement in your php script.

<?php
echo 'A text which is printed';
echo 
'Another text which should be printed';
?>

If you test this script on your webserver you get the following output.

A text which is printedAnother text which should be printed

The second string is just printed after the first one. Nothing else is actually written in your php script. First a string is printed then a second string is printed. To actually get a newline in the output you can use the html tag <br/>.

2. Print html code

Html code or be more specific the special characters < and > can be used in string without any problems. This allowes you to add the html tag <br/> at the end of your first string.

<?php
echo 'A text which is printed<br/>';
echo 
'Another text which should be printed';
?>

As expected you will get the following output.

A text which is printed
Another text which should be printed

If you check the source code in your browser you still see both strings in one line.

A text which is printed<br/>Another text which should be printed

3. Special characters in strings

If you add a apostrophe in your string you will get some problems as php recognize it as the end of your string, to early...

<?php
echo 'Dummy text ' with apostrophe';
echo '
Another text;
?>

As you see in this syntax highlighting php builds the string until the second apostrophe. To actually get a ' inside your string you can use double quoted string. These are strings build with quotes (") instead of apostrophes.

<?php
echo "Dummy text ' with apostrophe";
echo 
"Another text";
?>

Another way is to use escape sequences. In php strings the character \ is the escape character and is used to create such a escape sequence. In single quotes strings (strings with ' as delimiter) you can use the escape sequence \' to get a apostrophe into the string.

<?php
echo 'Dummy text \' with apostrohpe';
echo 
'Another text';
?>

The same way can be used to get a quote inside a double quoted string.

<?php
echo "Dummy text \" with apostrohpe";
echo 
"Another text";
?>

But now a question is rised how to get a single \ inside your string as it start a escape sequence. The solution is to escape the escape character.

<?php
echo "Double quoted with \\ im text";
echo 
'Single quoted with \\ im text';
?>

As it looks both single quoted and double quoted strings using the same functionality of escape sequences double quoted strings got more escape sequences. The following table shows the additional escape sequences which can be used in double quoted strings.

Table of escape sequences
Sequence Description
\n A newline
\r A carriage return, often used for network protocols.
\t A tab
\v A vertical tab, not often used
\f A form feed, even used less than \v
\$ The dollar character. Without these sequences php tries to find a variable and fills in the content of the variable.
\0 to \777 A character from the area of 0x000 to 0x1FF
\x0 to \xFF A character from the area of 0x00 to 0xFF

Questions about the chapter

1. What kind of strings exists?

There are 4 types of string but we only mentioned 2 types here. Besides double quoted strings and single quoted strings there are also called heredocs and nowdocs. An explaination of strings in php can be found in the manual of Strings.

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!