March 24, 2010

PHP Basic - Variables, Datatypes, Operators and Constants

Declare a variable

$my_variable = 3;

Name it in your script, when PHP first sees a variable's name in a script, it automatically creates the variable at that point.
If you don't initialize a variable in PHP, it's given the default value of null.

Data Types

Special Data Type

NULL : May only contain 'null' as a value, meaning the variable explicitly does not contain any value
Resource : Contains a reference to an external resource, such as a file or database

Be careful, NULL is a data type.

Testing the Type of a Variable

Returns true or false.

Changing a Variable's Data Type

To use settype() , pass in the name of the variable you want to
alter, followed by the type to change the variable to (in quotation marks).
Assigning different values to the variable can also change the variable's data type.

$test_var = 8.23;
settype( $test_var, "string" );
settype( $test_var, "boolean" );

after converting $test_var to a Boolean, it contains the value true
(which PHP displays as 1 ). This is because PHP converts a non-zero number to the Boolean value true.

Changing Type by Casting

$test_var = 8.23;
echo $test_var . "<br/>"; // Displays "8.23"
echo (string) $test_var . "<br/>"; // Displays "8.23"
echo (int) $test_var . "<br/>"; // Displays "8"
echo (float) $test_var . "<br/>"; // Displays "8.23"
echo (boolean) $test_var . "<br/>"; // Displays "1"

You can also cast a value to an integer,
floating-point, or string value using three PHP functions:
|# Functions|# Description|
|intval( value )|Returns value cast to an integer|
|floatval( value )|Returns value cast to a float|
|strval( value )|Returns value cast to a string|

intval() has another use: converting from a non?base-10 integer to a base-10 integer.
For example, this code convert base-16 number "ff" to base-10 number.

echo intval("ff", 16); // Displays "255"


Assignment Operators

For concatenation strings, using ".=" instead of ".+".

$a = "Start a sentence";
$b = "and finish it.";
$a .= $b;   // $a now contains "Start a sentence and finish it."

Bitwise Operators


Comparison Operators

Logical Operators

PHP considers the following values to be false :

Addtion to the "traditional" logical operators such as "&&" and "||",
PHP also have "and", "or" and "xor".

String Operators

There ’ s really only one string operator, and that ’ s the concatenation operator - .(dot).This operator simply
takes two string values, and joins the right - hand string onto the left - hand one to make a longer string.

Operator Precedence

Almost the same except that the logical operator: "and" > "xor" > "or"
has the lowest precedence.

$x = false || true; // $x is true
$x = false or true; // $x is false

In the second line, the "=" has higher precedence than "or".


Constants may only contain scalar values such as Boolean, integer, float, and string (not values such as arrays and objects).

Constants do not start with the dollar sign

To define a constant, use the define() function.

define("MY_CONSTANT","19"); // MY_CONSTANT always has the string value "19"
echo MY_CONSTANT; // Displays "19" (note this is a string, not an integer)

Share on Twitter Share the post
Qihuan Piao

Qihuan Piao

(aka kinopyo) is Chinese based in Tokyo. Software writer. He shares stories inspired him in this blog. His infamous line - "I feel calm when I kill those monsters, or people (in game)" shocks his friends deeply.

He also writes in Japanese and Chinese.