PHP is one of the simplest server-side languages out there, and it was designed primarily for web development. Learning PHP is good not only because it adds ...
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
PHP is free to download and use
3. What You Should Already Know
Before you continue you should have a basic understanding of the following:
HTML
CSS
JavaScript
4. What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code are executed on the server, and the result is returned to the
browser as plain HTML
PHP files have extension ".php"
5. 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
9. Offline Servers
• WAMP stands for
Windows/Apache / MySQL / PHP
• LAMP stands for
Linux/Apache / MySQL / PHP
10. XAMPP
XAMPP is a free and open source cross-platform web server package
X (meaning cross-platform/ multi-platform)
Apache
MySQL
PHP
Perl
A platform is a combination of hardware and software used to run software
applications.
11. Syntax!
A PHP scripting block always starts with
Syntax
<?php and ends with ?>
A PHP scripting block can be placed anywhere in the document.
13. Comments
// or # to make a single-line comment
/* and */ to make a large comment block.
<html><body>
<?php
// This is a comment
# This is a comment
/* This is a comment block */
?>
</body></html>
15. Variables
Variables are used for storing values, such as numbers, strings or function
results, so that they can be used many times in a script.
16. Variable Naming Rules
All variables in PHP start with a $ sign symbol. Variables may contain strings,
numbers, or arrays.
A variable name must start with a letter or an underscore "_“
A variable name can only contain alpha-numeric characters and underscores
(a-Z, 0-9, and _ )
A variable name should not contain spaces.
e.g-($myString)
17. Naming Rules
If a variable name should be more than one word, it should be separated with
underscore ($my_string)
Keywords may not be used as variable name
Variable names are Case-sensitive
Note: abc and ABC are distinct variable names
18. Assigning Values To Variables
$x=10; // numeric values
$y=“ABC”; or $y=‘ABC’; // string values
$x = 10;
Literals are any numbers ,text, or other information that directly represent a
value
Identifier Assign Sign Literal
19. Predefined variables
PHP uses predefined variables to provide access to important information
about the server and requests from a browser.
Since version 4.1.0 of PHP, the use of the new autoglobals, also known as
superglobals, have been the recommended way to access predefined
variables
22. Arithmetic Operators
As the name suggests PHP arithmetic operators provide a mechanism for
performing mathematical operations:
23. Operator Name Example Result
+ Addition $a + $b Sum of $a and $b.
- Subtraction $a - $b Difference of $a and $b.
* Multiplication $a * $b Product of $a and $b.
/ Division $a / $b Quotient of $a and $b.
% Modulus $a % $b Remainder of $a divided by $b.
++var Pre-increment ++$a Increments $a by one, then returns $a.
var++
Post-
increment
$a++ Returns $a, then increments $a by one.
--var
Pre-
decrement
--$a Decrements $a by one, then returns $a.
Var--
Post-
decrement
$a-- Returns $a, then decrements $a by one.
24. Increment and decrement operators
Prefix operators if listed before variable name
Prefix operator is incremented by 1 before the value is assign to new variable
Postfix operators if listed after a variable name
Postfix operators receives the value of old variable before it is incremented
by 1
25. Assignment Operators
The assignment operator is used to assign a value to a variable and is
represented by the equals (=) sign.
The assignment operator can also be combined with arithmetic operators to
combine an assignment with a mathematical operation (for example to
multiple one value by another and assigning the result to the variable).
26. Operator Name Example Is The Same As
= Assignment x=y x=y
+= Addition-Assignment x+=y x=x+y
-= Subtraction-Assignment x-=y x=x-y
*= Multiplication-Assignment x*=y x=x*y
/= Division-Assignment x/=y x=x/y
%= Modulo-Assignment x%=y x=x%y
27. Comparison Operators
The comparison operators provide the ability to compare one value against
another and return either a true or false result depending on the status of
the match.
The comparison operators are used with two operands, one to the left and
one to the right of the operator.
If you compare an integer with a string, the string is converted to a number.
If you compare two numerical strings, they are compared as integers.
28. Operato
r
Name Example Result
== Equal $a == $b TRUE if $a is equal to $b.
=== Identical $a === $b
TRUE if $a is equal to $b, and they
are of the same type. (introduced in
PHP 4)
!= Not equal $a != $b TRUE if $a is not equal to $b.
<> Not equal $a <> $b TRUE if $a is not equal to $b.
!== Not identical $a !== $b
TRUE if $a is not equal to $b, or they
are not of the same type. (introduced
in PHP 4)
< Less than $a < $b TRUE if $a is strictly less than $b.
> Greater than $a > $b TRUE if $a is strictly greater than $b.
<= Less than or equal to $a <= $b TRUE if $a is less than or equal to $b.
>=
Greater than or equal
to
$a >= $b
TRUE if $a is greater than or equal to
$b.
29. Logical Operators
Logical Operators are also known as Boolean Operators because they evaluate
parts of an expression and return a true or false value, allowing decisions to
be made about how a script should proceed.
Logic operators can be used to combine two or more comparison statements
combined into one statement to determine its status.
30. Operator Name Example Result
! Not ! $a TRUE if $a is not TRUE.
&& And $a && $b
TRUE if both $a and $b are
TRUE.
|| Or $a || $b
TRUE if either $a or $b is
TRUE.
31. String Operators
The PHP String concatenation operator is used to combine values to create a
string.
The concatenation operator is represented by a period/full stop (.) and can
be used to build a string from other strings, variables containing non-strings
(such as numbers) and even constants:
32. String Operators
There are two string operators.
The first is the concatenation operator ('.'), which returns the concatenation
of its right and left arguments.
The second is the concatenating assignment operator ('.='), which appends the
argument on the right side to the argument on the left side.
34. Control Structures
One of the main reasons for using scripting languages such as PHP is to build
logic and intelligence into the creation and deployment of web based data. In
order to be able to build logic into PHP based scripts, it is necessary for the
script to be able to make decisions and repeat tasks based on specified
criteria.
35. Control Structures
These PHP structures can be broken down into a number of categories as
follows:
Conditional Statements
Switch Statements
Looping Statements
36. Conditional Statements
Conditional statements provide the core of building decision making into
scripting languages such as PHP. Conditional statements essentially control
whether a part of a script is executed depending the result of a particular
expression (i.e whether an expression returns a boolean true or false value).
37. Conditional Statements
The PHP if Statement
Use this statement if you want to execute some code only if a specified
condition is true
Syntax
if (condition)
{ code to be executed if condition is true;}
38. If…else
The PHP if ... else Statements
The if statement above allows you to specify what should happen if a
particular expression evaluates to true. It does not, however, provide the
option to specify something else that should happen in the event that the
expression evaluates to be false
If you want to execute some code if a condition is true and another code if a
condition is false, use the if....else statement.
39. If..else
The PHP if ... else Statements
Syntax
if (condition)
{ code to be executed if condition is true;}
else
{ code to be executed if condition is false;}
40. syntax
The PHP if ... elseif…else… Statements
Use this statement if you want to select one of many blocks of code to be
executed
Syntax
if (condition 1)
{ code to be executed if condition is true;}
elseif (condition 2)
{code to be executed if condition is true;}
elseif (condition 3)
{ code to be executed if condition is true;}
else
{ code to be executed if condition is false;}
41. Conditional Statements
Switch Statements
Use this statement if you want to select one of many blocks of code to be executed
Syntax
switch(expression){
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
42. Looping Statements
Loop statements are the primary mechanism for telling a computer to
perform the same task over and over again until a set of criteria are met
Loops in PHP are used to execute the same block of code a specified number
of times or while a specified condition is true.
43. Looping Statements
PHP for loops
The for statement is used when you know how many times you want to
execute a statement or a list of statements.
Syntax
for (initialization; condition; increment)
{ code to be executed;}
44. Multiple for loop
Syntax
for (initialization 1; condition 1; increment 1){
for (initialization 2; condition 2; increment 2){
code to be executed;
}
}
45. The while loop
The while loop
The while statement will execute a block of code if and as long as a condition
is true.
Syntax
while (condition){
code to be executed;
}
46. The do...while Statement
The do...while Statement
The do...while statement will execute a block of code at least once - it then
will repeat the loop as long as a condition is true.
Syntax
do {
code to be executed;
}
while (condition);
47. break statements
break ends execution of the current for, foreach, while, do-while or switch
structure.
break accepts an optional numeric argument which tells it how many nested
enclosing structures are to be broken out of.
48. continue statements
continue is used within looping structures to skip the rest of the current loop
iteration and continue execution at the condition evaluation and then the
beginning of the next iteration.
continue accepts an optional numeric argument which tells it how many
levels of enclosing loops it should skip to the end of.
49. return statements
If called from within a function, the return() statement immediately ends
execution of the current function, and returns its argument as the value of
the function call. return() will also end the execution of an eval() statement
or script file.
50. require() & include()
The require() statement includes and evaluates the specific file.
require() includes and evaluates a specific file. Detailed information on how
this inclusion works is described in the documentation for include().
include()
The include() statement includes and evaluates the specified file.
51. PHP Arrays
An array can store one or more values in a single variable name.
PHP arrays allow you to store groups of related data in one variable (as
opposed to storing them in separate variables).
If you want to create many similar variables, you can store the data as
elements in an array.
Each element in the array has its own ID so that it can be easily accessed.
52. There are three different kind of arrays:
Numeric array - An array with a numeric ID key
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays
53. Numeric Arrays
A numeric array stores each element with a numeric ID key.
Can be assigned the ID key Manually or Automatically.
Each value of the array get a unique ID which is known as INDEX NUMBER.
Remember that the index number starts from 0.
Assign the ID key Automatically.
Syntax
Variable = array(“elem1"," elem2"," elem3");
element=KEY
54. Associative Arrays
When storing data about specific named values, a numerical array is not
always the best way to do it.
With associative arrays we can use the values as keys and assign values to
them.
When we want to store elements in array with some meaningful association
other than numbers, we use associative array
55. Associative Arrays
When storing data about specific named values, a numerical array is not
always the best way to do it.
With associative arrays we can use the values as keys and assign values to
them.
When we want to store elements in array with some meaningful association
other than numbers, we use associative array
56. Associative Arrays
Syntax I
variable=array(“Ele1"=>value,“Ele2"=>value,“Ele3"=>value);
Element names are case-sensitive, but type insensitive. It means that 'a'
differs from 'A' but '1' is the same as 1.
57. The foreach Statement
The foreach statement is used to loop through arrays.
Syntax
foreach (array as value){
code to be executed ;
}
58. Multidimensional Arrays
Each element in the main array can also be an array.
And each element in the sub-array can be an array, and so on.
A multidimensional array can contain arrays within itself and the sub arrays
contain more arrays within them
59. PHP Functions
A function is a block of code that can be executed whenever we need it.
A function is a named block of code that performs a specific task, possibly
acting upon a set of values given to it, or parameters , and possibly
returning a single value.
60. Functions
Functions save on compile time—no matter how many times you call them,
functions are compiled only once for the page.
They also improve reliability by allowing you to fix any bugs in one place,
rather than everywhere you perform a task, and they improve readability
by isolating code that performs specific tasks.
Functions in a PHP program can be either built-in or user-defined.
Regardless of their source, all functions are evaluated in the same way:
61. Create a PHP Function- user-defined
All functions start with the word “function()”
Name the function - It should be possible to understand what the function
does by its name. The name can start with a letter or underscore (not a
number)
Add a "{" - The function code starts after the opening curly brace
Insert the function code
Add a "}" - The function is finished by a closing curly brace
63. PHP Built-in Functions
PHP Array PHP Calendar PHP Date
PHP Error PHP Filter PHP FTP
PHP HTTP PHP Libxml PHP Mail
PHP Math PHP Misc PHPMySQL
PHP String PHP XML PHP Zip
PHP SimpleXML
PHP Filesystem
PHP Directory
64. Array Functions
These functions allow you to interact with and manipulate arrays in various
ways. Arrays are essential for storing, managing, and operating on sets of
variables.
78. array_rand()
Pick one or more random entries out of an array
Syntax:
array_rand ( array $input [, int $num_req ] )
num_req Specifies how many entries you want to pick -if not specified, defaults
to 1.
79. Mathematical Functions
These math functions will only handle values within the range of the integer
and float types on your computer
98. unset()
Unset a given variable / destroys the specified variables
Syntax
unset ($var ,$var ,$var... )
99. var_dump()
This function displays structured information about one or more expressions that
includes its type and value
Syntax
var_dump ($expression ,$expression ,$... )