9. PHP Syntax
PHP is programmer centric.
HTML and PHP are not same.
PHP is white space insensitive.
PHP is sometimes case sensitive.
Termination by semicolon (;)
Braces define blocks.
9
17. Commenting
<?php
// this is a single line comment.
# this is also a single line comment.
/* this is a
multi line
comment. */
?>
17
18. Variables
Variables are denoted with a leading ($) sign.
Value of variable is the most recent assignment.
Variable is assigned using (=) operator, to its left
is the variable and to right is the expression to
be evaluated.
18
19. Variables can, but do not need to be declared
before assignment.
Variables have no type other than the type of its
current value.
Variables should always start with an alphabet.
Numerical values for a variable can be INT,
HEX, OCT, FLOAT and e.
19
20. In variables for text we can use () as escape
character.
Boolean values is not case sensitive, TRUE,
TRuE, true, trUE, etc.
Prefix for HEX is "0x" and for OCT is "0".
Range of INT varies with respect to processor
bits, i.e. different for 32 and 64 bit processor.
20
21. $x = 'Hello';
$x = "Hello";
$x = <<<var
Hello
var;
Single quotes display as it is, whereas double
quotes and heredoc do interpretation.
21
22. Creating a variable
$x = 3;
Changing value
$x = $x + 5;
Changing value as well as type
$x = $x + 0.5;
22
23. Global Variables:
$x = "HELLO";
function test() {
global $x;
return $x;
}
echo test();
Super Global Variables: $_POST, $_GET, etc.
23
24. Outputting
echo "Hello World n Welcome to PHP";
echo "Hello World <br> Welcome to PHP";
echo "$var1 n $var2";
24
38. Some Important
Functions
strlen(): gives length of string.
strcmp(x,y): case sensitive string compare.
strcasecmp(x,y): case in sensitive string
compare.
strncmp(x,y,n): checks first n characters.
38
39. strtoupper(x): converts to uppercase.
strtolower(x): converts to lowercase.
trim(x): removes white space.
substr(x,m,n): creates substring of x from m to n
characters.
ord(x): gives int value for character.
39
40. count(x): gives no. Of elements in array.
isset(x): checks if has value.
sort(x): sorts array wrt values.
ksort(x): sorts array wrt keys.
array_merge(x,y): merge arrays to a new one.
40
47. PHP and Forms
HTTP is a stateless protocol
Data is sent using
GET
POST
47
48. To collect data for PHP processing we use super
global variables.
$_GET
$_POST
48
49. PHP and MySQL
Initialise a database connection
mysql_connect('host','username','pass');
mysql_select_db('dbname');
Firing a query
mysql_query('Query');
49
53. Cookie
Cookie is also super global variable.
It is used to store information about user and its
visit on user's computer.
It is located client side.
53