Understanding Strings in PHP

0
18

Hello everyone, this tutorial is about understanding and using strings in PHP.

So we all know that the strings are the sequences of characters or it could be a sentence or a phase. or maybe a word.

For example:

“String in PHP”

Valid example of strings in PHP would be as follows:

  1. $string = "This is a string.";

If you can notice from the codes above. This string is inside the double quotes. And now we will be using a single quoted string.

  1. $string = 'This is a string.';

All the statements from the codes above has the same output which is. “This is a string.”.

But singly quoted strings are the most treated literally, whereas double quoted strings replace variable with their values as well as specially interpreting certain character sequences. See the following codes:

  1. <?php<?php $variable = "name"; $statement = 'My $variable is Harley\\n'; print($statement); $statement = "My $variable is Harley\\n";?>

Output:

My $variable is Harley\n

My name is Harley

 

There are no artificial limits on string lengths – within the bounds of available memory, you ought to be able to make arbitrarily long strings.

Strings that are in the double quotes, for example “statement” are prepossessed in both the following ways by PHP:

  • The characters beginning with backslash (\) are replaced with special characters
  • Variables for example “$statement” are replace with string representations of their values.

The escape-sequence replacements are:

  • \n – replace by the newline character
  • \r – replace by the carriage-return character
  • \t – replace by the tab character
  • \$ – replace by the dollar sign itself
  • \” – replace by a single double-quote
  • \\ – replace by a single backlash

That’s all.

if you have questions regarding these article entitled as “Understanding Strings in PHP  feel free to ask us by commenting below or visit on our contact page. Thank you.

Facebook Comments
(Visited 22 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here