Unit-4
Unit-4
UNIT-4:PHP
PREPARED BY: PROF. MAYUR PRAJAPATI
What is PHP ?
etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
(Version 7.4.3)
PHP can create, open, read, write, delete and close files on the server
Output
Comments in PHP
Example:
<?php
// This is a single-line comment
# This is also a single-line comment
/* This is a multiple-lines
comment block
*/
?>
Case sensitivity in PHP
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions,
A variable starts with the $ sign, followed by the name of the variable
Variable names are case-sensitive ($age and $AGE are two different variables)
Variables in PHP
The value can be assigned to variable by
$variable_name=value;
Integers
Doubles
Booleans
NULL − is a special type that only has one value: NULL.
Strings
Arrays − are named and indexed collections of other values.
Objects − are instances of programmer-defined classes, which can package up
both other kinds of values and functions that are specific to the class.
Resources − are special variables that hold references to resources external to
PHP (such as database connections).
datatype.php
output
Operators
Arithmetic : (+,-,/,*,%).
Relational : (<,>,<=,>=,==)..
Logical : ( != ,&&,||).
Assignment : ( =).
Increment : ( ++ ).
Decrement : (--).
operator.php
output
Various String Operations
output
2nd Example
$str1 = “Hello”;
$str2 = “Friends”;
?>
4) Reverse string:
$str1 = “Hello”;
$str2 = “Friends”;
O/P : olleH.
Decision Making
If..else..
If.elseif..else
switch
Cont..
If ($a>$b)
$msg= “a is greater”;
else
$msg = “b is greater”;
Conditional Statements
conditions
is true
do...while - loops through a block of code once, and then repeats the loop
dowhile.php
output
Looping Statements Example
forfibo.php
output
Looping Statements Example
forpattern.php
output
Looping Statements Example
foreach.php
output
Arrays
It is collection of similar type of elements.
1. Indexed Array
2. Associative Array
3. Multidimensional Array
Arrays
PHP Indexed Array
PHP index is represented by number which starts from 0. We can store number,
string and object in the PHP array. All PHP array elements are assigned to an
index number by default.
There are two ways to define indexed array:
st
1 way: $season=array("summer","winter","spring","autumn");
2nd way:
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
Arrays
PHP Associative Array
PHP allows you to associate name/label with each array elements in PHP using
=> symbol.
Ex:
$salary=array("A"=>"1","B"=>"2","C"=>"3");
Assoindexarray.php
output
Arrays
PHP Multidimensional Array
<?php 1 A 4000
$emp = array 2 B 5000
(
array(1,"A",4000), 3 C 3000
array(2,"B",5000),
array(3,"C",3000)
);
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $emp[$row][$col]." ";
}
echo "<br/>";
} ?>
Functions
Syntax of function is
function function_name()
Function body
}
Three types of functions :
Output
Forms
Form gives GUI the user.
The value of the form elements can be obtained by the PHP script using
$_GET and $_POST variables.
If we collect the values using GET method then $_GET variable is used in
the PHP script.
Note: if the GET method is used then all variable names and values will be
displayed in the URL as a Query String.
But if we use POST method then these values will not be displayed in the
URL.
Both GET and POST create an array (e.g. array( key => value, key2 =>
value2, key3 => value3, ...)). This array holds key/value pairs, where keys
are the names of the form controls and values are the input data from the
user.
Form Example
Phpform.html
output
Welcome.php
Welcome_get.php
Get Vs. Post
When to use GET?
Information sent from a form with the GET method is visible to everyone
(all variable names and values are displayed in the URL). GET also has
limits on the amount of information to send. The limitation is about 2000
characters. However, because the variables are displayed in the URL, it is
possible to bookmark the page. This can be useful in some cases.
GET may be used for sending non-sensitive data.
Note: GET should NEVER be used for sending passwords or other
sensitive information!
Get Vs. Post
When to use POST?
Information sent from a form with the POST method is invisible to others
(all names/values are embedded within the body of the HTTP request) and
has no limits on the amount of information to send.
Moreover POST supports advanced functionality such as support for
multi-part binary input while uploading files to server.
However, because the variables are not displayed in the URL, it is not
possible to bookmark the page.
Getdata.php
PHP file
PHP File System allows us to create file, read file line by line, read file
character by character, write file, append file, delete file and close file.
The PHP fread() function is used to read the content of the file. It accepts two
arguments: resource and file size.
The PHP fwrite() function is used to write content of the string into ffile.
file1.php
myfile.txt output
PHP Writing to File example
file2.php
output
EXCEPTION HANDLING:
PHP 5 has an exception model similar to that of other programming languages.
Exceptions are important and provides a better control over error handling.
2. Throw − This is how you trigger an exception. Each "throw" must have at
least one "catch".
1. An exception can be thrown, and caught ("catched") within PHP. Code may
be surrounded in a try block.
2. Each try must have at least one corresponding catch block. Multiple catch
blocks can be used to catch different classes of exceptions.
<?php
try {
$error = 'Always throw this error';
throw new Exception($error);
// Continue execution
echo 'Hello World';
?>
In the above example $e->getMessage function is used to get error message.
There are following functions which can be used from Exception class.
Encapsulation
Message passing
Inheritance
Polymorphism
Example using class and object
oop.ph
output p
output
Example using constructor and destructor
condes.ph
p
output
THANK YOU……