VARIABLE AND TYPE IN PHP

Although variables are not declared to be type-specific in PHP, PHP still has a common set of data types:
boolean  integer  float  string  array  object  resource  NULL
Determining the current type of a variable:
A series of type-testing functions exist to determine the current type of variables:
gettype(varname)
returns type name, such as 'string'
is_int()  is_integer()  is_long()  is_null()  is_numeric()
is_object()  is_real()  is_string()  is_scalar()  is_bool()
empty()  isset()
PHP type comparison tables
Special debugging / variable-display functions:
print_r()  var_dump()  var_export()

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed as:
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
To assign values or expressions to variables, the standard assignment equal-sign operator ( = ) is used
$var = "Bob";
$Var = "Joe";  // different variable
$Long_variable_numeric_name = 47;

$4site = 'not yet';   // invalid; starts with a number
$_4site = 'not yet';  // valid; starts with an underscore
$täyte = 'mansikka';  // valid; 'ä' is (Extended) ASCII 228

Special Relationship Between Strings and Variables
String constants can be defined in one of three common ways:
Inside Single Quotes: 'One type of string'
Inside Double Quotes: "Another type of string"
Using special "heredoc" syntax (discussed later)

Data inside Single-quoted strings are taken literally; ie., everything is treated exactly as it is typed
Data inside Double-quoted strings are treated in a special way in relationship to variable references and other standard formatting characters:
If a variable is referenced inside a double-quoted string, its value is automatically substituted.
"Escaped" characters are interpreted: Table of "Escaped" characters
$var1 = 'This is a test';
$var2 = 27;

$var3 = "$var1 $var2\n";  // "This is a test 27
                             "

$var4 = '$var1 $var2\n';  // '$var1 $var2\n'
If variable names can be clearly delineated in the double-quoted string sytax (it is not obvious where the variable name ends and literal text following the variable name begins), a variable name can be surrounded with curly-braces { }, or separated from the rest of the text using concatenation (the period . operator):
$var1 = 'ABC';
$var2 = "Value is $var1xyz";         // "Value is "
$var3 = "Value is {$var1}xyz";       // "Value is ABCxyz"
$var4 = 'Value is ' . $var1 . 'xyz'; // "Value is ABCxyz"

All string constants (single- or double-quoted) can be automatically continued onto multiple lines:
$var1 = 'This is a long variable
that is continued onto multiple lines.';

$var2 = "This is a long variable with another
variables defined inside it: $var1\n";

Displaying Values
Values can be displayed (output) using three methods:
echo,  print(), and printf()

echo string arg1 [, string argn...];
echo outputs all values following it. It is not actually a function (it is a language construct) so you are not required to use parentheses with it.
echo "This is a test\n";

$var1 = 'Test string';
$var2 = 75;

echo "The value of var1 is $var1\n";
echo "The value of var2 is $var2\n";
echo "Multiple variables displayed: $var1 $var2\n";
echo "This is a value that
is written on multiple lines,
including variable $var2 references.
";
echo 'This',$var2,'that'; // "This75that"

print() is in some ways similar to echo, although it can be used as a function and could be included in more complicated expressions
echo "This is a test\n";

$var1 = 'Test string';
$var2 = 75;

print "The value of var1 is $var1\n";
print ("The value of var2 is $var2\n");

$ret = print "Hello World"; // $ret will equal 1
Discussion of differences between echo and print

printf() is one member of a family of string formatting functions. It is based on the syntax of the sprintf() function.
$var1 = 123.456;
$var2 = 255;
$var3 = 'text';

printf ("<pre>%d %05d %5.2f %'*-10s %o %b %x</pre>",
            $var1,$var2,$var1,$var3,$var2,$var2,$var2);
123 00255   123.46 text****** 377 11111111 ff

Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment