php tutorial - PHP String - php programming - learn php - php code - php script



  • A string is a collection of characters.
  • String is one of the data types supported by PHP.
  • The string variables can contain alphanumeric characters.
  • Strings in PHP are declared in two ways:
    1. Single quoted (‘ ‘)
    2. Double quoted (“ “)

php script Syntax :

$variable_name =”value” (or) ‘value’ ;
click below button to copy the code. php tutorial - team

  • If you define the string in single quotes, the content will be displayed in output browser without any changes.
  • If you define the string in double quotes, the content will be interpreted and the output of the program will be displayed.

Sample Code :

<!DOCTYPE html>
<html>
    <head>
        <title>Datatype-NULL</title>
    </head>
    <body>
        <?php
            $var1='Wikitechy is the Top $Ten Technical Website';
	    $var2="Wikitechy is the Top $Ten Technical Website";
	    echo $var1; 
	    echo "<br>"; 
	    echo $var2; 
        ?>
    </body>
</html>
click below button to copy the code. php tutorial - team

Code Explanation :

Code Explanation for String In PHP

  1. Here in this statement we define the variable “$Ten” and assign the value as “10”.
  2. In this Example, the “$var1” is a variable starts with the “$” sign with their variable name. In this example the variable holds the string value in single quotes (‘ ‘).
  3. “$var2” is a variable name that’s holds the string value using double quotes (“ “) .
  4. echo $var1 – is the echo statement displaying the value of the variable by displaying in the browser without any changes because we defined the ‘var1’ in single quotes.
  5. echo $var2 – is the echo statement that displays the value of the variable by interpreting (by changing) the output & displaying in the browser.

Sample Output :

output for String In PHP

  1. Here in this output the statement “Wikitechy is the Top $Ten Technical Website” has be declared in the variable “var1” in single quotes so the output will be displayed without interpreted.
  2. Similarly, the statement “Wikitechy is the Top 10 Technical Website” has be declared in the variable “var2” in double quotes so the output will be displayed after the interpretation of the variable “$Ten” whose value “10” has been assigned here.


Related Searches to php string