PHP String

In this chapter you will learn:

  1. What is a string
  2. Syntax to define string in PHP
  3. Example - Create a string in PHP
  4. Multline strings

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/* jav a  2  s  . c  o  m*/
$myString = " 
    demo
    from
    java2s.com
";   

echo $myString;
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. PHP Single vs double quotation string
  2. Example - Difference between single quote and double quote
Home » PHP Tutorial » PHP String
PHP String
PHP Single vs double quotation string
PHP String Escape Sequences
PHP Variable substitution in String
PHP String Element