PHP notes for Java programmer (2) - types and variables
PHP Language Reference Notes - Types, Variables and Constants
1. It is not possible to mix XHTML declaration with PHP. Once way to solve this is to use <?php echo(”<?xml version=”1.0″ encoding=”UTF-8″?>\n”); ?>
2. A PHP instruction is terminated by a semicolon (except for code blocks such as an if block).
3. Three types of comments are possible - /*comment*/, //comment and #comment. Be careful when you mix them!
4. PHP supports eight basic types. They are boolean, integer, float, string, array, object, resource and NULL.
5. PHP types are weak and are dynamically typed. PHP variables are prefixed by dollar sign($).
6. Type conversion is easy! You can cast types or use settype() method. For example $intvar = (int)”100″ is a valid statement in PHP!. This is equivalent to $v1 = “100″; settype($v1,”integer”);
7. Boolean literals true and false are case insensitive.
8. Integer overflow causes a variable change its type to float!
9. PHP has no native support for unicode strings.
10. String literals can be expressed in 3 forms - single quoted, double quoted and using heredoc syntax.
11. Single quote strings don’t evaluate the string content. Hence if variables are inside strings, they are not interpreted.
12. Double quote strings are evaluated. Hence if there are variable or special characters, the corresponding value is output. For example echo “hello $val” will print “hello world” if $val=”world”.
13. String characters can be accessed using index operation. For example, $v1[4];
14. Use complex syntax in double quoted strings for evaluating complex expressions!. For example, $v1 = “hell”; echo “{$v1}o”; - The output is hello.
15. For string concatenation using dot(.) operator. For example “Hello”.” World”
16. By default arrays in PHP are associative in nature. Hence array(”0″,”1″,”2″) and array(”0″ => “0″, “1″ => “1″, “2″ => “2″) are identical.
17. $v1[] = “100″; extends an array by adding the element “100″ to it. The key will be maxkey + 1;
18. PHP supports callback functions.
19. PHP variable scope rules are a bit strange. A globally declared variable is not visible inside a function snippet. For example following won’t print “hello” since $a inside test() is a different variable!
<?php
$a = “hello”;
function test() {
echo $a;
}
test();
?>
20. Keyword global can be used to refer global variables in functions. For example,
<?php
$a = “hello”;
function test() {
global $a
echo $a; // prints hello
}
test();
?>
21. Another alternative is to use $GLOBALS superglobal! In the above example, $GLOBALS[’a'] returns the value of $a.
22. Another interesting PHP variable property is that variables declared in a code block is visible below the code block! Consider the following example,
<?php
$check = true;
if($check) {
$a = “hello”;
}
echo $a; // prints hello
?>
23. PHP has something called variable of variable. For example, $a = “hello”; $$a = “world”; echo “$a $hello”; // This prints “hello world”.
24. In PHP, constants are declared using define. There is no need for a dollar sign to refer a constant. For example, define(”PI”,3.14);echo PI;
25. PHP has a large number of magic constants. Values of some these depend on the context they are used. For example, echo __LINE__; will give you the current line number!


