WWW PHP 2003 1 Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2
Comments PHP Shell Style: # C++ Style: // C Style: /* */ $value = $p * exp($r * $t); # $value = $p * exp($r * $t); // /* */ 3 # (//) # (//) PHP?> <?php $d = 100; # d 100?> <?php echo $d?> 100 /* */ /* */ PHP /* $j = 1; $i = 100; /* */ */ 4
/* */ comment out $i = 100; /* $j = $i + 20; $k = $i * $j; */ $i ++; 5 Literals 2001, 0xFE 3.14, 1.0E-6 Hello, A String, true, false, null 6
Identifiers name PHP 1. _ ASCII 0X7F 0XFF 2. 0 9 7 Keywords PHP PHP if, else, do, true,, $argc, $_POST,, PHP array, echo,, PHP 8
Variables PHP $ $amount $this_is_friday $_here $not valid $3Com $@someplace 9 C/C++ PHP PHP $var = 3; $var = Hello ; $var = array(10, 20, 30); $var = 5; # # # # 10
Constants PHP define() define(name, expression); name expression define(pi, 3.1415); echo PI; # 3.1415 define(message, How are you doing? ); echo MESSAGE; # How are you doing? 11 Data Types PHP boolean integer floating number string array object NULL resource 12
boolean true false if ($isok) { } PHP false false 0 0.0 0 NULL true 13 integer PHP -2,147,483,648 2,147,483,648 2003, -123, +78 0x 0X 0XFF, 0x16, 0xff, -0xABCD 0 0755, -0123 14
floating number PHP C double 15 1.7 10-308 1.7 10 308 3.14, 0.0015, -7.8 0.314E1, 0.314e1, 13.5E-4, -2.1e6 15 string PHP Thanks God, it s Friday escape sequence Hello, world! \\ \ \ 16
\n newline \r carriage return \t horizontal tab \\ backslash \$ dollar sign \{ right brace \} brace \[ right bracket \] bracket \ double quote \ooo ooo 1 3 ASCII \xhh hh ASCII 17 echo Don t stop! ; echo <h1 align=\ center\ > ; echo First line\nsecond line ; echo Ho\x6De ; echo C:\\Windows ; Don t stop <h1 align= center > First line Second line Home C:\Windows $value = 100; echo The value is $value ; The value is 100 echo Don\ t stop! ; echo <h1 align= center > ; echo First line\nsecond line ; echo The value is $value ; echo C:\\Windows ; Don t stop <h1 align= center > First line\nsecond line The value is $value C:\Windows 18
array PHP PHP indexed array 40 25 3.14 Hello 0 1 2 99 indices associative array key keys lastname firstname sex values male 19 $myarray[0] = 40; $myarray[1] = 50; $myarray[3] = 20; $myarray[4] = 10; NULL $myarray 40 50?? 20 10 0 1 2 3 4 $myform[ lastname ] = ; $myform [ firstname ] = ; $myform [ sex ] = male ; keys lastname firstname sex values male $myform 20
PHP array() $myarray = array(40, 50, NULL, 20, 10); # $myarray 40 50 NULL 20 10 0 1 2 3 4 # $myform = array( lastname =>, firstname =>, sex => male ); keys lastname firstname sex $myform values male 21 NULL PHP 1. NULL $var = NULL; # $var 2. 3. unset() unset($var); # $var 22
resource $resourse $resourse = db_connection(); # db_connection() $resourse $resourse db_query($resource, $query); # qb_query() $query 23 $var is_bool($var); is_float($var); is_array($var); is_null($var); is_int($var); is_string($var); is_object($var); is_resource($var); var_dump($var) $var 24
25 Example $a + $b $a - $b $a * $b $a / $b $a % $b -$a Name Addition Subtraction Multiplication Division Modulus negation Result Sum of $a and $b. Difference of $a and $b. Product of $a and $b. Quotient of $a and $b. Remainder of $a divided by $b. Multiply $a by -1 +$a assertion The as $a 26
PHP 5 + 4 # 9 (int) 17 + 3.14 # 20.14 (float) 27 PHP 5 + 3.14 # 8.14 (int) 9 Lives 1 # 8 (int) 3.14 Pies * 2 # 6.28 (float) 1.0E3 Points of Light + 1 # 1001 (float) 28
Example Name Result $a & $b And Bits that are set in both $a and $b are set. $a $b Or Bits that are set in either $a or $b are set. $a ^ $b Xor Bits that are set in $a or $b but not both are set. ~ $a $a << $b $a >> $b Not Shift Shift right Bits that are set in $a are not set, and vice versa. Shift the bits of $a $b steps to the (each step means "multiply by two") Shift the bits of $a $b steps to the right (each step means "divide by two") 29 PHP ~$a $a $a & $b $a $b $a ^ $b $a $b $a $b $a << $b $a >> $b $a $b 30
Assignment Operator Example Result Example Result $a = $b $a is set to be the value of $b $a &= $b $a = ($a & $b) $a += $b $a = ($a + $b) $a = $b $a = ($a $b) $a -= $b $a = ($a - $b) $a ^= $b $a = ($a ^ $b) $a *= $b $a = ($a * $b) $a <<= $b $a = ($a << $b) $a /= $b $a = ($a / $b) $a >>= $b $a = ($a >> $b) $a %= $b $a = ($a % $b) $a.= $str $a = ($a. $str) 31 Example Name Effect ++$a Pre-increment Increments $a by one, then returns $a. $a++ --$a $a-- Post-increment Pre-decrement Post-decrement Returns $a, then increments $a by one. Decrements $a by one, then returns $a. Returns $a, then decrements $a by one. 32
Example $a == $b $a === $b $a!= $b $a <> $b $a!== $b $a < $b $a > $b $a <= $b $a >= $b Name Equal Identical Not equal Not equal Not identical Less than Greater than Less than or equal to Greater than or equal to Result TRUE if $a is equal to $b. TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only) TRUE if $a is not equal to $b. TRUE if $a is not equal to $b. TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only) TRUE if $a is strictly less than $b. TRUE if $a is strictly greater than $b. TRUE if $a is less than or equal to $b. TRUE if $a is greater than or equal to $b. 33 == ===!=!== 3.14 == 3.14 # true 3.14 === 3.14 # false 3.14 === 3.14 # true 3.14 == 3.14 Pies # true 3.14 === 3.14 Pies # false 3.14!= 3.14 # false 3.14!== 3.14 # true 3.14!== 3.14 # false 3.14!= 3.14 Pies # false 3.14!== 3.14 Pies # true 3.14 <> 3.14 # false 34
3.14, 20, 1.0e-6,, Hello, 3 books,, 35 Example Name Result $a and $b $a && $b $a or $b $a $b $a xor $b And Or Xor TRUE if both $a and $b are TRUE. TRUE if either $a or $b is TRUE. TRUE if either $a or $b is TRUE, but not both.! $a Not TRUE if $a is not TRUE. 36
Lazy Evaluation PHP Lazy Evaluation 1 == 1 or echo This is skipped true $result = fopen($filename) or exit(); exit() $result = $flag and mysql_connect(); $flag mysql_connect() 37 $string1. $string2 $string1 $string2 $string1.= $string2 $string1 = $string1. $string2; Hello,. world! # Hello, world! The value is. 3 # The value is 3 The value is. 3 # The value is 3 $a = 3.14; The value is. $a # The value is 3.14 $a = It s ; $a.= ok. ; # $a It s ok. 38
Type Casting C/C++ (int) (integer) (float) (real) (bool) (boolean) (string) (array) Integer Floating point Boolean String Array (object) Object 39? : (conditional_expr)? true_expr : false_expr conditional_expr true true_expr false_expr $a = (3 == 3 )? equal : not equal ; # $a = equal $a = (3 === 3 )? equal : not equal ; # $a = not equal 40
Error Control Operator PHP @ /* Intentional file error */ $my_file = @file ('non_existent_file') or die ("Failed opening file: error was '$php_errormsg'"); // this works for any expression, not just functions: $value = @$cache[$key]; // will not issue a notice if the index $key doesn't exist. 41 Execution Operator `external_command` PHP ` external_command WWW Unix $output = `ls -al`; echo "<pre>$output</pre>"; 42
Associativity Operators, or xor and right print = += -= *= /=.= %= &= = ^= ~= <<= >>=? : && ^ & non-associative ==!= ===!== non-associative < <= > >= << >> + -. * / % right! ~ ++ -- (int) (float) (string) (array) (object) @ right non-associative [ new 43 PHP ( ) echo 1 + 2 * 3; # 7 echo (1 + 2) * 3; # 9 echo 1 + (2 + 3) * 4; # 21 associativity right echo 4 2-1; # 1 (not 3) echo (int)(float) 3.14; # 3 echo (float)(int) 3.14; # 3.0 44