PHP String

Description

A string variable is created using quotation.

Syntax

In PHP we have two syntax to define strings

$myString = 'string value';

or

$myString = "string value";

The first form uses the single quotation and the second form uses the double quotation.

Example

The following code creates a string in PHP.


<?PHP
  $myString = 'hello from java2s.com';   
  print($myString);
?>

The code above generates the following result.

In this example, the string literal is enclosed in single quotation marks ('). You can also use double quotation marks ("), as follows:


<?PHP
  $myString = "hello from java2s.com";  
  print($myString);
?>

The code above generates the following result.

Multline strings

To specify multline strings in PHP, insert newlines into the string literal between the quotation marks:


<?PHP/*from w  w  w  .j a va2s.  co m*/
$myString = " 
    demo
    from
    java2s.com
";   

echo $myString;
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone