View
217
Download
1
Category
Preview:
Citation preview
PHPArū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 <?php ... ?> <script language=“php”> ... </script>
Short tags <? ... ?> ir <?= ... ?> <% ... %> ir <%= ... ?>
Not recommended due to compatability reasons
Output
Main function to output data: echo
echo “Hello”; echo 12345;
ĮTERPIMAS Į HTML. PAVYZDYS
Instruction separation
Semicolon (;) is used to separate instructions from one another
<?php echo “foo”; echo “bar”;?>
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
Code Result
Main PHP data types
Boolean Logical true/false
Integer 1, 2, 3, 4…
Float 3.1415, 0.21513
String ‘Text’
Array
Object
Resource
NULL No value
BOOLEAN Logical true or false value
$a = true; $a = FaLsE;
Possible FALSE values: FALSE 0 0.0 ‘’ 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 = 2 + 2
- 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
==
Equal 5==8 false
!= Not equal 5!=8 true
<>
Not equal 5<>8 true
> More 5>8 false
< Less 5<8 true
>=
More or equal 5>=8 false
<=
Less or equal 5<=5 true
Logical
&& AND
|| OR
! NOT
Exercise
Task no 1
1. Multiply N by 32. Add 15 to the result3. Find module of
division by 4 of the result
4. Add 2 to the result5. Raise the result to
the second power (Res2)
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
Recommended