2. What is PHP?
• PHP is an acronym for "PHP: Hypertext Preprocessor"
• PHP is a widely-used, open source scripting language
• PHP scripts are executed on the server, and the result is
returned to the browser as plain HTML
• PHP is free to download and use
• PHP is used in WordPress and Facebook
3. What Can PHP Do?
• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, and close files
on the server
• PHP can collect form data
• PHP can send and receive cookies
• PHP can add, delete, modify data in your database
• PHP can be used to control user-access
• PHP can encrypt data
4. BASIC PHP SYNTAX
• PHP scripting block starts with <?php and end with ?>
• Each code line in PHP must end with a semicolon. The semicolon is a
separator and is used to distinguish one set of instructions from another
• There are two basic statements to output text with PHP: echo and
print.
• PHP files have a file extension of ".php", ".php3", or ".phtml" .The
default extension is “.php”
• In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and
user-defined functions are not case-sensitive.
• But all variables are case sensitive.
5. Variables
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
• Variable names are case-sensitive ($age and $AGE are two different variables)
• We use php echo statement to output data to screen
• Variable type is automatically assigned according to value of the variable.
• We use dot operator to concatenate two or variables together
7. Global scope
• A variable declared outside a function has a GLOBAL
SCOPE and can only be accessed outside a function.
• We need to use “global” keyword to access global
variable within in a function.
• PHP also stores all global variables in an array called
$GLOBALS[index]. The index holds the name of the
variable. This array is also accessible from within
functions and can be used to update global variables
directly.
8. Local scope
• A variable declared within a function has a LOCAL
SCOPE and can only be accessed within that function
9. static scope
• Normally, when a function is completed/executed, all of
its variables are deleted.
• But we can use “static” keyword to store the value of a
variable even after executing.
• So each time we called the function it picked previous
value of the variable from memory and use it.
10. Comments
• “//” or “#” => for single line comment
<?php
// This is a single-line comment
# This is also a single-line comment
?>
• “/*” and “*/” => for multiline comments
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
17. echo and print
echo
▪ output data to the screen.
▪ echo has no return value
▪ echo can take multiple
parameters.
▪ echo is marginally faster than
print
print
▪ output data to the screen
▪ print has a return value of 1
▪ print can take one argument
18. PHP DATA TYPES
▪ PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
19. PHP BOOLEAN
▪ A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
20. PHP NULL VALUE
▪ Null is a special data type which can have only one value: NULL.
▪ A variable of data type NULL is a variable that has no value assigned
to it.
▪ If a variable is created without a value, it is automatically assigned a
value of NULL.
▪ Variables can also be emptied by setting the value to NULL:
<?php
$x = "Hello world!";
$x = null;
?>
21. PHP settype() function
▪ The settype() function converts a variable to a specific type,
▪ Syntax
settype(variable, type);
variable Required. Specifies the variable
to convert
type Required. Specifies the type to
convert variable to. The
possible types are: boolean,
bool, integer, int, float, double,
string, array, object, null
22. TYPECASTING IN PHP
▪ typecasting is a way to utilize one data type variable into the different data type.
• (int), (integer) - cast to integer
• (bool), (boolean) - cast to boolean
• (float), (double), (real) - cast to float
• (string) - cast to string
• (array) - cast to array
• (object) - cast to object
23. Arrays
▪ An array stores multiple values in one single variable.
▪ In PHP, the array() function is used to create an array.
▪ In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric index
• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing one or more arrays
24. Indexed array
▪ There are two ways to create indexed arrays:
▪ The index can be assigned automatically (index always
starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
▪ or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
25. Associative arrays
▪ Associative arrays are arrays that use named keys that you
assign to them.
▪ There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
▪ or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
26. PHP strcmp() function
▪ The strcmp() function compares two strings
▪ Syntax
Strcmp(string1,string2)
• This function returns:
• 0 - if the two strings are equal
• <0 - if string1 is less than string2
• >0 - if string1 is greater than string2
Parameter Description
string1 Required. Specifies the first string to compare
string2 Required. Specifies the second string to compare