Creating and Accessing Strings

  • Single Quotation String

$mystring= 'This is Single Quotation String';

  • Double Quotation String

$mystring= "This is Double Quotation String";

Single and double quotation marks work in diffrent ways. If you enclose a string is in single quotation marks, PHP use the string exactly as typed. However, double quotaion marks give you a couple of extra features.

  • Any variable names within the string are parsed and replaced with the variables value.
  • You can include specail characters in the string by escaping them.

Here are some example to make these difference clear:

Example:

<?php
$a='SOFTAID';
$b="PHP is a server side scripting language";
$br="<br/>";// for breacking line
echo "Hello, $a! <br/>";
echo 'Hello, $b!<br/>';
?>

Output is:
Hello, SOFTAID!
Hello, $b!

PHP String Functions

PHP gives you several string functions that let you:

chr():  Returns a character from a specified ASCII value.
Example:

<?php
$br="<br/>";
echo $br.chr(65);
echo $br.chr(97);
?>

Output is:
A
a

chop():  Removes whitespace or other character from the right end other a string.
Example:

<?php
$br="<br/>";
$str="SOFTAID INFOWEB";
echo chop($str,"WEB");
echo $br.$str;
?>

Output is:
SOFTAID INFO