• Your Cart is Empty
  • Cart
  • Log In

How to create a PHP string

A string is a series of characters. There are exactly 256 different characters possible. The present stable versions of PHP - PHP4 and PHP5, have no native support for Unicode. There aren't string length limitations in PHP except the server's available memory and the configuration of the php.ini settings file.

Strings in PHP

The string, like a variable, will have to be created first. There are two ways to use a string in PHP - you can store it in a function or in a variable. In the example below, we will create a string twice - the first time storing it in a variable, and the second time - in a function, in our case - an echo.

A sample string usage:

<?php
$myString = "This is a string!";
echo "This is a string!";
echo $myString;
?>

The output of this PHP file will be:

This is a string!This is a string!

In this example, the first string is stored in a variable, while the second one is not. If you store your strings, you will be able to use them later on, without the need to specify them once more.

Heredoc string usage:

<?php
$myLargeString = <<<EOT
Example of string
spanning multiple lines
using heredoc syntax.
EOT;

echo $myLargeString;
?>

PHP strings and echo

In order to output a string you can use the PHP echo function. You can place either a string variable or you can use quotes to create a string that the echo function will output. You can output HTML using a PHP string. However, you must be careful when using HTML code or any other string that includes quotes. You can set a string in three different ways - using single quotes, using double quotes or using the heredoc method.

PHP strings with single and double quotes

Like all values in PHP, strings too will have to stay in quotes when specified. There are two ways of quoting when you can specify a string in PHP - single quotes or double quotes. If you want to use a single quote within the string you have to escape the single quotes (otherwise known as apostrophes) with a backslash. If you choose to use double quotes, note that this method allows many special escape characters to be used unlike the case with the single-quote string. Once again, a backslash is used to escape a character. When a string is specified with single quotes, all variables in the string will not be treated as variables, but as text, and will not be passed to the output.

Differences in string quoting:

<?php
$myVar = 'red';
$myString1 = "My apple is $myVar!";
$myString2 = 'My apple is $myVar!';

echo $myString1; // My apple is red!
echo $myString2; // My apple is $myVar!
?>

PHP strings with heredoc

The two methods above are traditional for creating strings in most programming languages. PHP has introduced 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 properly code your string. In order to use heredoc, you need to open a string using <<< and some identifier, for example TEXT. Then you should type your text. To close the heredoc, repeat the identifier followed by a semicolon (TEXT;). Note that the closing sequence TEXT; must occur on a line by itself and cannot be indented.

A Heredoc PHP string

<?php
$my_string = <<<TEXT
My
apple
is
red.
My Apple is red!
TEXT;
echo $my_string;
?>

The output of this example will be:

My Apple is red.My Apple is red!

Even though multiple lines were used in the example, in order to have multiple lines in the output, you will have to use the HTML </br> tag.

When a string is specified in double quotes or with heredoc, variables are parsed within it.

Escaped characters:*

sequence meaning
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\\ backslash
\$ dollar sign
\" double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation
* special characters in the duble quoted and heredoc strings.

Useful PHP string functions

Besides the echo function, there are also several other functions you can use with PHP strings:

  • strlen() function - this function will show you how many characters are used in a string. It will take the whole string as an argument.

    A sample usage of the strlen() function

    <?php
    $myString="Hello!"
    echo strlen($myString)
    ?>

    The result of this function will be:

    6

  • str_replace() - a very useful function, which can modify your string, replacing just a part of it. Here is a simple example to demonstrate how this function works:

    A sample usage of the str_replace function:

    <?php
    $myString = "My apple is red";
    echo str_replace("red", "blue", $myString);
    ?>

    This will search for all instances of "red" in the string and replace them with "blue". The output of the above is:

    My apple is blue
  • strtoupper() function - this function will convert all lowercase characters in a string to uppercase.

    A sample usage of the strtoupper() function

    <?php
    $myString = "hello";
    echo strtoupper($myString);
    ?>

    The above will output:

    HELLO
  • ucfirst() function - this function will convert the first letter of a string from lowercase to uppercase

    A sample usage of the ucfirst() function:

    <?php
    $myString = "hello";
    echo strtoupper($myString);
    ?>

    The above will output:

    Hello
  • trim() function - this useful function will strip a string of the white spaces left in the beginning or the end.

    A sample usage of the trim() function:

    <?php
    $myString = " Hello! ";
    echo strtoupper($myString);
    ?>
    The above will output: Hello!

PHP strings and NTC Hosting

If you are a web developer or a webmaster and are using PHP for your projects, we, at NTC Hosting, will provide you with a complete solution. Our web hosting plans support both PHP4 and PHP5. You are not limited to choosing just one, but can change PHP versions all the time via our easy to use Web Hosting Control Panel.

With our PHP-optimized packages you can not only run all your PHP projects, but also extend them using a Perl - or a Python based script