PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




String datatype in PHP


Tutorials »Php »

Topic

How to define a string variable and assign value?



Explanation

A string is a sequence of characters. A string can be delimited by single quotes or double quotes. The declaration of a string variable is same as that of other variable. The only difference is that the value that you want to store in a string variable must be enclosed in inverted commas or quotes.

For Example:

$str1 = "This is a string datatype variable";
$str2 = 'This is also a string datatype variable';


String declared within double-quotes are subject to variable substitution and escape sequence handling, while string declared within single-quotes will not perform variable subsitution and escape sequence handling.

For example:

$str="Integer";
echo "String\t$str\n";

RESULT:
String Integer

This displays "String" followed by a space and then "Integer" followed by a newline. Here variable substitution is performed on the variable $str and the escape sequences are converted to their corresponding characters.

echo 'String\t$str\n';

RESULT:
String\t$str\n

In this case, the output is exactly "String\t$str\n". There is no variable substitution or handling of escape sequences, the string within the inverted commas are printed as such.

String Concatenation is a process of adding two strings. This is done by attaching one string to the end of another string. This is done by using '.' (period) operator as shown below

$str1="Hello";
$str2="World";
echo "$str1"."$str2";


RESULT:
HelloWorld

This displays value of $str1 as "Hello" followed by value of $str2 as "World".





Other Links

web hosting