Types of Variables:
All the examples on this page can be found in the php sample site
PHP has 5 data types it supports:
-
Integer,
- Double (real numbers),
- Strings (text),
- Arrays,
- Objects (for classes)
A variable does not have to be "declared" before it is used. The value assigned to it will determine the PHP datatype. PHP can swap between data types without problem or error depending on the type of the value stored in that variable at that given time.... so creating an integer and then using it in a formula that would alter it to a Double would work flawlessly without error. Be aware of this and be careful when manipulating your variables.
In general, variables follow the following rules:
- All variables begin with $
- PHP variables ARE case sensitive... so $PicKle is NOT the same variable identifier as $pickle.
- PHP can change variable datatypes on the fly (so be careful)
- '=' is assignment variable, not "is equal to"...
'==" is "is equal to"
$x = 1; //var x is integer assigned value of 1 $y = "this is a string"; //var y is a string with contents of 'this is a string' $z = array(); //var z is an array (more on arrays in a bit) $filevar = "/html/joke1.txt"; //var filevar is string w/ contents of file location $imagevar = "/html/headsw.gif"; //var imagevar is string w/ contents of picture $x = "girls rule"; // var x is now a string with the value "girls rule"
Positioning in Fusion
Variables can then be used anywhere a constant would be. In our "Read File or Display Image example on the sample site "phpinfusion.php" page, it would have looked like
Integers:
Any number without referred to without decimals is an integer.
$x=12; $y=3; $z=144;
Results in browser:
Double (Real) Numbers:
A numerical value is referred to as a double or real number if the value has a decimal place (even if the decimal value is .00000), or when the result of a mathematical equation has a non-integer result.
Example 1:
Results in browser:
Since PHP can alter datatypes on the fly based on user input or a statement expression, you should be aware of what expressions can do to your variables. This will become more apparent when you are entering information in your forms and processing it.
Example 2: Look at this example of how PHP can alter the database of variable originally assigned as integers:
Results in the browser:
Strings (Text):
PHP can manipulate strings easily. It can concatenate, chop up, parse strings.
$str1 = "I am a valid PHP string"; // quote type does not matter as long as they match at both ends $str2 = 'I am a valid PHP string'; $str3 = $str1.$str2; //var str3 is assign the concatenation of vars str1 and str2 echo $str3;
the above code block would look like this in Fusion:
Example 1:
Results in browser:
Example 2: Sometimes, you don't even need to use the concatenate in an echo statement to display the values
$a=22; $b=4; $c=1; $d="fellas"; $e="gals";
echo("Out of $a pets on the GotFusion team, most are $d. I like it most when one the only $b $e answer my posts<br>");
// echo w/ mixed variable datatypes echo("Sometimes, it takes ($b-2) $e to whip ($a-$b) $d into shape :) <br>");
//same echo but this time concatenate the value (perform match) into the echo string echo("Sometimes, it takes". ($b-2)." $e to whip ". ($a-$b)." $d into shape :) <br>");?>
Results in browser:
Arrays:
Variables can also be arrays. or even arrays of arrays...
Just repeat the following tips
- PHP begins indexing arrays at 0, not 1 as is in other languages and as HTML references
- each element of an array can be a different datatype
- Each element of an array can be an array (2D arrays like tables), and each element of each array can be another array (3D array like a cube)... while the flexibility makes it very powerful, it's also very easy to lose track of where/what element is.
$x = array(1, 3, 5, 7, 9); //var x is an array with 5 elements
/* $x[0] is 1, ... $x[3] is 7, .... */
To append to the end of an array, refer to the array with no index in the brackets:
$x[] = 13; //now array $x contains ( 1, 3, 5, 7, 9, 13)
Arrays can also be referred to by "indexing" or "naming the elements"
$i=4; $y = $x[$i]; //var y is now assigned value 9
Displaying indexed values in browser:
So placing this code in the INSERT HTML box
Gives this result in the browser:
PHP also has many functions that can check or manipulate datatypes, arrays, and manipulate text strings. Be sure to refer to your materials for more information regarding Variables and datatypes
next lesson - Flow Control
Did you find this tutorial useful? Would like to view all of our tutorials and support pages? Join the NetObjects Fusion Users Group Community
|