Data

String

PHP

 

Defining String

PHP 5
$str1 = 'Hello World!';

Strings can simply be defined by setting a string value to a variable.

 

String Literals

PHP 5
$name1 = 'Sam';   // Single quoted
$name2 = "Sam";   // Double quoted

// NowDoc
$name3 = <<<'EOT'
Sam
EOT;

// HereDoc
$name4 = <<<EOT
Sam
EOT;

There are 4 String Literal representations:

  • Single quoted
  • Double quoted
  • NowDoc
  • HereDoc
 

String Literal: Single quoted

PHP 5
$name = 'Bob';

$greeting1='Hi! I\'m $name'; // Hi! I'm $name

$greeting2='Hi! \nI\'m $name'; //Hi! \nI'm $name

$greeting3='Hi!
I\'m $name';
/*
Hi!
I'm $name
*/

In Single quoted Strings the only characters you need to escape are single quote (\') and back slash(\\).

Variable names will not be expanded (replaced by value) and they are considered as literal strings.

You can expand your Single quoted string to multiple lines.

Single quoted strings can be multiline.

 

String Literal: Double quoted

PHP 5
$name = "Bob";

$greeting1="Hi! I\'m $name"; // Hi! I\'m Bob

$greeting2="Hi! \nI\'m $name"; 
/*
Hi!
I\'m Bob
*/

$greeting3="Hi!
I\'m $name";
/*
Hi!
I\'m Bob
*/

Unlike Single quoted Strings, PHP interprets escape sequences for special characters (\n, \r, \t, ...) in Double quoted strings.

Variable names will be expanded (replaced by value) in Double quoted strings. Your variables inside string can simply be written $var_name or ${var_name}.

You can expand your Double quoted string to multiple lines.

Single quoted strings can be multiline.

 

String Literal: Nowdoc

PHP 5
$name = 'Bob';

$greeting = <<<'NOW'
Hi!
I'm $name!
NOW;

/*
Hi
I'm $name!
*/

Nowdoc Strings are similar to Single quoted strings, in a sense that no parsing is done inside a Nowdoc.

You can use any wording instead of NOW in the sample code.

No escaping is needed inside the string.

This syntax is great for having code strings inside your code.

 

String Literal: Heredoc

PHP 5
$name = 'Bob';

$greeting = <<<HERE
Hi!
I'm $name!
HERE;

/*
Hi
I'm Bob!
*/

Heredoc Strings are similar to Double quoted strings, in a sense that PHP will parse the string and expand variables (replace by value).

You can use any wording instead of HERE in the sample code.

No escaping is needed inside the string.

This syntax is great for having code strings inside your code.

 

Character Escape

PHP 5
$text1 = "Sam\'s age:\t21";  // Sam's age:   21

To specify special characters or characters that make ambiguity for parsing String Literals, you need to escape them. This is a list of escaping characters:

  • \" Single quote
  • \" Double quote
  • \\ Backslash
  • \n Newline
  • \r Carriage return
  • \R Line break: matches \n,\r and \r\n
  • \v Vertical tab
  • \t Tab
  • \b Backspace
  • \f Form feed
  • \a Bell
  • \xXX Latin-1 character ("\x4E" -> "N")
  • \OOO Octal Latin-1 character ("\116" -> "N")
  • \u{XXXX} Unicode character ("\u{004E} -> "N")

Unicode Character escape is introduced in PHP7.

 

String Length

PHP 5
$str1 = "Hello World!";

$length = strlen($str1);  // 12

The strlen() function returns the number of characters inside a string.

 

Get Character

PHP 5
$str1 = "Hello World!";

$firstCh = $str1[0];  // H

Strings in PHP can be seen as zero-based character arrays.

You can get a character in a string at any index by specifying the index of the character inside brackets ([]).

 

Set Character

PHP 5
$str1 = "Hello World!";

$str1[1] = '3';  // $str -> "H3llo World!"

You can change a character in String at the specified index, same as changing an element inside an Array.

 

Concatenating Strings

PHP 5
$str1 = "Hello";
$str2 = "World!";

$str3 = $str1 . " " . $str2;  // Hello World!

You can concatenate strings using the . operator.

 

Find Substring

PHP 5
$str1 = "Hello World";

$index = strpos($str1, 'llo');   // 2

The strpos() function searches for the index of the first occurrence of some string inside another one.

If the substring does not exist, it returns false.

 

Get Substring

PHP 5
$str1 = "Hello World!";

$substr1 = substr($str1, 6);    // World!
$substr2 = substr($str1, 6, 2); // Wo

Function substr() gets the start (inclusive) and the length of substring and returns the substring for those parameters.

If you don't specify the length of the substring, slice() will return the substring from the starting point to the end of the string.

 

Convert to String

PHP 5
$num = 12;

$str1 = strval($num);  // 12

The strval() function returns the string value of a variable.