PHP Programing language

adplus-dvertising
PHP String
Previous Home Next

The PHP-String is a great deal to so it is important to have a basic understanding of PHP strings.

String Creation:

The creation before you can use a string you have to create it. A string can be using 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:

<?php
$my_string = " The r4rtechsoft is a very useful sites!";
echo " The r4rtechsoft is a very useful sites!";
echo $my_string;
?>

In this example the first string will be stored into the variable $my_string, and 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.

Output:

The r4rtechsoft is a very useful sites!

Single quotes String creation

The String far we have created the strings using double-quotes, but it is correct to creation a string using single-quotes, otherwise known as apostrophes.

PHP Code:

<?php
$my_string = 'The r4rtechsoft is a very useful sites!';
echo 'The r4rtechsoft is a very useful sites!';
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: \' !

Double quotes String creation:

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:

<?php
$newline = "Newline Begins \n";
$return = "To return the newline\r";
$tab = "again tab to newine \t";
$dollar = "how to dollar sigh representing \$";
$doublequote = "And how to doublequote using  \"";
?>

String creation for here doc:

The two methods above are the traditional 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 properly code your string!

Here's how to do it:

PHP Code:

<?php
$my_string = <<<TEST r4rtechsoft.com
that is the most useful site.
TEST;
?>
echo $my_string;

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

  1. Use <<< and some identifier that you choose to begin the heredoc. In this example we chose TEST as our identifier.
  2. Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was TEST;
  3. The closing sequence TEST; must occur on a line by itself and cannot be indented!
  4. 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.
Previous Home Next