140 likes | 322 Views
PHP – Data Types. PHP supports 8 data types: boolean, integer, float, string (scalar types) array, object (compound types) resource, NULL (special types). PHP – booleans. Use keywords true or false Both are case insensitive Can typecast a value to boolean, using (bool) or (boolean)
E N D
PHP – Data Types • PHP supports 8 data types: • boolean, integer, float, string (scalar types) • array, object (compound types) • resource, NULL (special types)
PHP – booleans • Use keywords true or false • Both are case insensitive • Can typecast a value to boolean, using (bool) or (boolean) • Usually not necessary: automatic conversion to boolean if boolean value is needed
PHP – booleans • When converted to boolean, the following values are considered false: • boolean false, integer 0, float 0.0, empty string, string “0”, an array with 0 elements, an empty object, the special type NULL • everything else is true
PHP – integers • Positive or negative • Decimal, hex, or octal notation • $num1 = 879; // decimal notation • $num2 = 0xAB45; // hex notation • $num3 = 047; // octal notation
PHP – floating point numbers • Positive or negative • $a = 1.234; • $b = 1.2e3; • $c = 7E-10; • /* If an integer value overflows, it is automatically converted to a float */
PHP – strings • A string is a series of characters (there is no size limitation) • Single quotes • Double quotes • heredoc syntax
PHP – strings: single quotes • No expansion of variables and escape sequences, except ‘ and \: • // Outputs: Arnold said: "I'll be back"echo 'Arnold said: "I\'ll be back"'; • // Outputs: You deleted C:\*.*?echo 'You deleted C:\\*.*?'; • // Outputs: You deleted C:\*.*?echo 'You deleted C:\*.*?';
PHP – strings: double quotes • Expansion of variables and escape sequences • \$ $ • \\ \ • \” “ • \t, \n, … tab, new line, .. • $num value of $num
PHP – strings: Heredoc syntax • <<< followed by an identifier • Same identifier to close the quotation, followed by a semicolon ( ; ) • Closing identifier must begin in the first column of the line • No space or tab before or after the semicolon
PHP – strings: Heredoc syntax • Variables are evaluated • Escape sequences are evaluated
PHP – strings: Heredoc • $name = “PHP”; • $version = 4.2; • echo <<<EOTMy name is "$name“\n\n\n\n My version is $versionEOT;
PHP – strings: Heredoc • $str = <<<EODExample of string<BR>spanning multiple lines<BR>using heredoc syntax.EOD;
PHP – back quotes • Can use back quotes to execute UNIX command (same as Perl) • $today = `date`; • $today contains output of UNIX command data • Can also do • echo `date`;