PHP Arūnas Liuiza
PHP 101
What is PHP? Widely popular dynamic interpreted opensource programming language, aimed for web development Syntax is simmilar to C, Perl, Java PHP code snippets can be inserted to a HTML document
PHP Syntax
Inserting to HTML Long tags... Short tags ir Not recommended due to compatability reasons
Output Main function to output data: echo echo “Hello”; echo 12345;
Į TERPIMAS Į HTML. P AVYZDYS
Instruction separation Semicolon (;) is used to separate instructions from one another
Comments PHP has three types of comments Single line (till the end of line or ?>) : // comment # comment Multi-line /* comment carries to another line and another… */
Variables Symbolic name, that can have a value assigned to it Starts with dollar ($) sign $first First symbol has to be a letter or an underscore (_) $start $_with_underscore Can also contain numbers $p123_44dlsdm_ $_1234ps Variable names are Case Sensitive $Pirmas != $pirmas $DuKartai != $dukartai
Variables. Examples $foo = 15; echo ‘foo is ‘.$foo; $foo = “Peter”; echo ‘foo is ‘.$foo; $foo = “Peter”; $foo = “John”; echo ‘foo is ‘.$foo; foo is 15 foo is Peter foo is John CodeResult
Main PHP data types Boolean Logical true/false Integer 1, 2, 3, 4… Float , String ‘Text’ Array Object Resource NULL No value
B OOLEAN Logical true or false value $a = true; $a = FaLsE; Possible FALSE values: FALSE ‘’ ir ‘0’ Empty array Object without members NULL Anything else - TRUE
Type casting PHP does not require strict definition of variable data type – type is converted according to context
Operators PHP operators are divided to: Arithmetical Value assigning Comparison Logical
Arithmetical + Sum$a = Subtract$a = 4 – 2 * Multiply$a = 2 * 2 / Divide$a = 4 / 2 % Division module$a = 5 % 2 (=1) ++ Add 1++$a -- Subtract 1--$a. Connect two strings $a = “raga”. “nosis”
Value assigning = $x = $y += $x += $y$x = $x + $y -= $x -= $y$x = $x - $y *= $x *= $y$x = $x * $y /= $x /= $y$x = $x / $y.= $x.= $y$x = $x. $y %= $x %= $y$x = $x % $y
Comparison == Equal5==8 false != Not equal5!=8 true <> Not equal5<>8 true > More5>8 false < Less5<8 true >= More or equal5>=8 false <= Less or equal5<=5 true
Logical && AND || OR ! NOT
Exercise
Task no 1 1. Multiply N by 3 2. Add 15 to the result 3. Find module of division by 4 of the result 4. Add 2 to the result 5. Raise the result to the second power (Res 2 ) 6. Subtract 1.5 from the result 7. Divide result by 3 Write a program that executes the operations one by one, outputing the result after each operation