PHP 5 Strings

Just like any other programming language, Strings in Php are also a collection of alphanumeric characters, enclosed in single quotes for simple string data and double quotes for complex string data.


Creating PHP 5 Strings

There are many different ways in which we can create a string in Php. Let's start with the most simplest way,

<?php

$srt1 = "This is a String";
$str2 = 'This is also a String';

?>

We can define a string using double quotes as well as single quotes.

But, what if the string has a single or double quote in it, then what?

<?php

$srt1 = "This is "a" String";
$str2 = 'This is also 'a' String';

?>

Both the above string definitions will result in error, as we cannot include a double quote in the string, if the string is defined inside double quotes.

But it will work, if the string contains double quotes, but it is defined using single quotes and vice versa, for example,

<?php

// string inside double quotes, with a single quote
$srt1 = "This is 'a' String";

// string inside single quotes, with a double quote
$str2 = 'This is also "a" String';

?>

There is one more simple technique to deal with the quotes problem, it is known as Escaping Special Character, which can be done using a backslash.

Let's take an example,

<?php

// escaping double quote using backslash
$srt1 = "I\"ll handle this.";

// escaping single quote using backslash
$str2 = 'I\'ll handle this.';

echo $str1;
echo "\n";  // new line
echo $str2;

?>

I"ll handle this. I'll handle this.

As evident from the examples above, it's quite simple to declare and define a string variable in PHP 5. Just be careful of the quotes and a few other special characters like $. Let's take an example for $ special character too.

<?php

// string with $
$srt1 = "pas$word";

echo $str1;

?>

NOTICE : Undefined variable pas

Running the above code, will show that only the part of the string before the $ symbol will get stored in the variable, while $word is treated as a variable, which is not assigned any value, hence, PHP prints a NOTICE. Now let's try to use backslash to escape it,

<?php

// string with escaped $
$srt1 = "pas\$word";

echo $str1;

?>

pas$word


Creating String using Heredoc method

If we want to define a multiline string, using the here doc syntax is the best and the easiest technique.

Using this methodology we can create strings with more than one line, without string concatenation.

Also, there is no requirement of escaping double or single quotes, if we use the here doc methodology to define strings.

Let's take an example:

<?php

$str = <<< EOT
    Take a right from the T point,
    and the go straight to fins the "Shop"
EOT;

echo $str;

?>

Take a right from the T point, and the go straight to fins the "Shop"

Here EOT is just a synonymn for end of text, which means nothing. These 3 characters are just to mark the start and end of the multiline string.

You can use EOF or maybe SQL if you use this to define your multiline SQL query, like,

<?php

$tablename = "products";

$str = <<< SQL
    Select * from $tablename
    Where product_name = "widgets"
SQL;

echo $str;

?>

Select * from products Where product_name = "widgets"


Creating String using Nowdoc method

The Nowdoc method of creating a string is similar to the Heredoc method, with only one difference, which is that the string is not parsed, hence if we want to add a value to our string using some other variable`, like we did in the SQL query string above, we cannot do that with Nowdoc.

Let's take an exmple,

<?php

$hername = "Selena";

$str = <<< 'LOV'
    This is my declaration of love,
    for $hername
LOV;

echo $str;

?>

This is my declaration of love, for $hername

The variable in the string is not translated into its value.


Printing the String variable

Printing a string variable is simple, let's take 2 examples, in the first we will only print the string variable using the echo method, and in the second, we will append a string variable with some other text in the echo method, enclosed in single and double quotes.

<?php

$hername = "Selena";

// only the string variable
echo $hername;

// for new line
echo "\n";

// string variable with text and single quote
echo '$hername, I love you!';

echo "\n";

// string variable with text and double quote
echo "$hername, I love you!";

?>

Selena $hername, I love you! Selena, I love you!

See the difference, single quotes does not parse the text inside it, hence the variable is not translated into its value, while in case of double quotes, it is.