Phpnotes
Phpnotes
Variables
A variable is a special container that you can define to "hold" a value. Variables are fundamental
to programming. preceded by a dollar sign ($). Variable names can include letters, numbers, and the
underscore character (_). They cannot include spaces. They must begin with a letter or an underscore.
print (2 + 4);
$a;
$a_longish_variable_name;
$2453;
$sleepyZZZZ;
<?php
7: $undecided = 3.14;
8: print gettype( $undecided ); // double
9: print " is $undecided<br>"; // 3.14
10: settype( $undecided, 'string' );
22: ?>
23: </body>
24: </html>
By placing the name of a data type in parentheses in front of a variable, you create a copy of that
variable's value converted to the data type specified.
<?php
7: $undecided = 3.14;
8: $holder = ( double ) $undecided;
9: print gettype( $holder ) ; // double
10: print " is $holder<br>"; // 3.14
11: $holder = ( string ) $undecided;
12: print gettype( $holder ); // string
13: print " is $holder<br>"; // 3.14
: $holder = ( integer ) $undecided;
20: $holder = ( boolean ) $undecided;
You have seen the assignment operator each time we have initialized a variable. It consists of the
single character =.
$x = 4;
$x = $x + 4; // $x now equals 8
$x = 4;
$x += 4; // $x now equals 8