Dr. Andrew Besmer
<?php
$x = "Hello World";
$x = 0;
$x = true;
<?php
$aardvark = 55;
if ($aardvrak < 50)
{
echo "It is less than 50!";
}
elseif ($aardvark > 50) //no space!
{
echo "It is more than 50!";
}
else
{
echo "It must be 50!";
}
//Outputs: "It is less than 50!"
var_dump($aardvrak);
var_dump((int)$aardvrak);
mixednumbercallbackarray or objectbooleantrue or false alternatively B = {0, 1}
false2
false00.0"" or "0"array()NULLtrue including -1integer+ or - indicating sign
0b indicates binary0 indicates octal0x indicates hex<?php
$bin = 0b10100011; //163 in decimal
$oct = 0123; //83 in decimal
$dec = 123; //123 in decimal
$hex = 0x64; //100 in decimal
var_dump($dec == $oct); //is bool(false)
integer size can be determined using the constant PHP_INT_SIZE
PHP_INT_MAX can be used for the maximum value<?php
var_dump(011901); //Decimal 9!
integer overflow results in using a float using E notationfloat aka doublefloatE notation or number with decimal placeThis can lead to confusing results: for example,
floor((0.1+0.7)*10)will usually return7instead of the expected8, since the internal representation will be something like7.9999999999999991118....3
<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
if(abs($a-$b) < $epsilon) {
echo "true";
}
NaNNaN using is_nan()string<?php
$name = "Andrew Besmer"; //My name uses 13B
$name = "&rew Besmer"; //My 1337 name uses 11B
' character to start and end the string<?php
$name = 'Andrew Besmer';
<?php
$greeting = 'Hello';
$name = '$greeting Andrew Besmer';
echo $name; //Outputs: $greeting Andrew Besmer
Pat O'Neal?\ can be used as an escape character\' makes ' and \\ makes \<?php
$name = 'Pat O\'Neal';
echo $name; //Outputs: Pat O'Neal
" can also be used to specify start and end of strings<?php
$greeting = "Hello";
$name = "$greeting Andrew Besmer";
echo $name; //Outputs: Hello Andrew Besmer
Other common escape characters
| Sequence | Meaning |
|---|---|
| \r | CR |
| \n | LF |
| \t | HT |
| \v | VT |
| \\ | backslash |
| \$ | dollar sign |
| \" | double-quote |
Remember that a CRLF in HTML does nothing!
<<<IDENTIFIER which will accept a string until seeing the IDENTIFIER;<<<"IDENTIFIER" more explicitly explaining what will happen in the string<?php
$greeting = "Hello!";
$longText = <<<EOF
$greeting
This is some longer text.
All of this will wind up in the string.
If can go on for many lines.
EOF;
<<<'IDENTIFIER' instead' explicitly describes expected functionality<?php
$greeting = "Hello!";
$longText = <<<'EOF'
$greeting
This is some longer text.
All of this will wind up in the string.
If can go on for many lines.
EOF;
$name[0]NULL<?php
$emptyString = ''; //NULL
. operator NOT the + operator which many other languages usearrayobjectresourceNULLresourceNULLNULL if
NULLunset()NULL is not case senesitiveis_null()<?php
$test = "100"; //A string
$test = $test + 10; //An integer
$test = $test + 10.5; //A float
$test = $test + "15 hundred"; //A float 135.5
$test = 100 + "15 hundred"; //An integer 115
(int), (integer)(bool), (boolean)(float), (double), (real)(string)(array)(object)(unset)(binary)boolean see earlier for how values are determined to be true or false<?php
var_dump((bool) ""); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
boolean to integer
false becomes 0true becomes 1float to integer
undefined or NaN no errors are thrown<?php
echo (int) ( (0.1+0.7) * 10 ); //Is 7 not 8!object to float results in a noticeinteger to float can result in loss of precisionboolean to string
false is ""true is "1"float or integer to string
E notationobject to string
"Object"array to string
ArrayNULL to string
""string to number
., e, or E in it becomes integer otherwise floatstring used then rest discarded0String conversion to numbers from php 6
<?php
$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "-1.3e3"; // $foo is float (-1299)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)