Sunteți pe pagina 1din 47

 PHP stands for Hypertext Pre-Processor.

 It was created by Rasmus Lerdorf in 1994.


 It’s a server side scripting language.
 Create Highly Dynamic Webpages.
 PHP is a widely-used, open source scripting language
 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"
Six major characteristics :-
 Familiarity
 Simplicity
 Efficiency
 Security
 Flexibility
 Open-Source
2 ways to use PHP and make a dynamic web application is :-
 We can set server on our own and create a “Local Host”
o XAMPP
o WAMP
o NetBeans

 Go for online hosting


 PHP Syntax 
<?php
echo “Hello World !”;
?>
 We can use PHP in HTML 
<div style=‘<?php echo “border:2px solid black”; ?>’> Red </div>
 Comments 
// Single Line
/* Multiple Line */
# Single Line
 Variables 
$var = “value”;
Rules for writing Variables
• Prefixed by $
• Start with _ or letter but not digits
• Spaces are not allowed
• Numbers need no quotes
Conjugation in PHP
• We use . (Period) for Conjugation
<?php
$Name=“Karan”;
echo “Hey I am” . $Name;
?>
function test_input($data)
{
$data = trim($data);
$data = htmlspecialchars($data);
return $data;
}
 Trim will remove unnecessary tabs, spaces and new lines
 Htmlspecialchars will convert html entities to HTML escaped code
<script>alert(“Hacked !”)<script>
&lt;script&gt;alert(“Hacked !”)&lt;/script&gt;
Hashing
MD5 & sha1
Arrays are of 3 types :-
 Numeric
 Associative
 Multi-dimentional
<?php
$ar = array (“Bob”, “Tim”, “Jim”);
echo $ar[1];
$arr = array (“Bob”=>“Audi”, “Tim”=>“Bently”);
echo $arr[‘Bob’];
$arrr = array (“Names”=> array(“Bob”, “Tim”, “Jim”), “Age”=> array(21,31,32));
echo $arrr[‘Names’][2];
?>
Sorting an Array
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
Echo() Print()
Echo has no return value and can not be used in Print has a return value of 1 and thus can be used
an expression. in an expression.
Echo can take multiple parameters. Print can take only one parameter.

Echo is marginally faster than Print. Print is marginally slower than Echo.

<?php <?php
$name=“Karan”; $name=“Karan”;
echo “He is $name”; print “He is $name”;
echo ‘He is $name’; print ‘He is $name’;
?> ?>
Explode() vs Implode()

<?php
$arr = array('Hello','World!','Beautiful','Day’);
echo implode(" ",$arr);
?>

 Hello World! Beautiful Day!

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str,3));
?>

 Array ( [0] => Hello [1] => world. [2] => It's a beautiful day. )
foreach loop
foreach ($array as $value)
{
code to be executed;
}

foreach ($array as $key=>$value)


{
code to be executed;
}
Super Global Variables
Several predefined variables in PHP are "superglobals", which means that they are always accessible,
regardless of scope - and you can access them from any function, class or file without having to do anything
special.
Example :-
 $GLOBALS
 $_SERVER – Has information of script name, path, browser etc.
 $_REQUEST – collects data after submitting form
 $_POST – Has all POST values
 $_GET – Has all GET values
 $_COOKIE – Has all Cookies Variables
 $_SESSION – Has all Session Variables

Include/require
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies
it into the file that uses the include statement.
 Also known as HTTP Cookie, Web Cookie or Browser Cookie
 Cookies are small, randomly encoded text files that identifies a user.
 setcookie(name, value, expire);
 setcookie(“User”, “Karan”, time() + (seconds));
 To access Cookie use Global Variable $_COOKIE [“Cookie_Name”];
 setcookie(name, value, time()-3600 );
 Note: Cookies are stored on Client’s System.
 Sessions is a way to make data accessible across various pages of a website.
 To start a session : session_start();
 Session variables are set with the PHP global variable:
$_SESSION[“Session_name”].
 print_r($_SESSION);
 session_unset()  Removes all Session Variables
 session_destroy()  Destroy the Session
 Make sure the form uses the method=“POST”
 Enctype=“multipart/form-data”
 <input type=“file” name=“abc” id=“xyz”>
 file_uploads = On in php.ini
$_FILES [‘filenameattr’][‘temp_name’]
$_FILES [‘filenameattr’][‘name’]
$_FILES [‘filenameattr’][‘size’]
$_FILES [‘filenameattr’][‘type’]
$_FILES [‘filenameattr’][‘error’]
$myfile = fopen(“File", “Mode") or die("Unable to open file!");
fread($myFile,filesize(“File"));
fclose($myfile);
fgets($myfile); - Reads a single line
feof($myfile); - Checks the end of file
fgetc($myfile);
fwrite($myfile,$text);
Two directives to be supplied :-
• SMTP – server address
• Sendmail_from – own email
Mail(“Email”,”Subject”,”$msg”, “headers”);
 CMS stands for Content Management System.
 Collaborative creation of document and other contents.
 Allows end user to submit an article entered as plain text.
 Control Panel
American company operating a worldwide online payments system that supports
online money transfers and serves as an electronic alternative to traditional paper
methods like cheques and money orders. The company operates as a payment
processor for online vendors, auction sites, and other commercial users, for which
it charges a small fee in exchange for benefits such as one-click transactions and
password memory.
 IPN stands for Instant Payment Notification

POST transaction data to listener script

Listener Script handles data

Notification is triggered when a transaction related event occurs.

Listener page reacts according to the content of Message received.


Some common Application takes in response to IPNs :-
1. Trigger Order Fulfilment
2. Enable online service or media download
3. Update list of customers
4. Update accounting records
5. Etc.
 Model View Controller
 Model is responsible to manage data
 View is responsible to display data
 Controller receives request and invokes
model to perform the operation and then
sends data to view
Advantages of using MVC model :-
 Models & Views are separated making it more flexible
 Models & Views can be changed separately
 Each module can be tested & debugged saperately
Controller
Controller
<?php
class Site extends CI_Controller
{
public function index()
{
echo "This is my first page";
// $this->hello();
}
public function hello()
{
echo "This is my first custom function";
}
}

Localhost/ci/
Localhost/ci/index.php/site/hello
Localhost/ci/
Model
Controller Model
<?php <?php
class Site extends CI_Controller class Math extends CI_Model
{ {
public function index() public function addStuffs()
{ {
echo "This is my first page"; return (1+2);
$this->add(); }
} }
public function add()
{
$this->load->model(“math”);
echo $this->math->addStuff();
}
}

Localhost/Ci/
View
Controller View/view_home.php
<?php <!DOCTYPE html>
class Site extends CI_Controller <html>
{ <head>
public function index() <title>
{ <?php echo $title; ?>
echo "This is my first page"; </title>
$this->home(); </head>
} <body>
public function home() <h1>Welcome To My First View</h1>
{ </body>
$data[“title”]=“Welcome”; </html>
$this->load->view(“view_home”,$data);
}
}
 It is a frame work.
 In computer systems, a framework is often a layered structure indicating what
kind of programs can or should be built and how they would interrelate.
 Code Igniter is a powerful PHP framework with very small footprints.
 It was created by EllisLab
 Open source
Some important features of Code Igniter are :-
 MVC based
 Extremely Light Weight
 Several Database support
 Query Builder Database Support
 Form & Data Validation
 Session Management
 File Uploading Classes
 Pagination
 Search engine friendly URLs
 Flexible URIs Routing
 Large Library of “Helper” function
$this->load->database('group_name');
Where group_name is the name of the connection group from your config file.
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";

$this->load->database($config);
URI : Uniform Resource Identifier is a string designed for unambiguous identification of
a resource
application/config/routes.php
www.example.com/blog/user/34
$route['blog/Joe'] = 'blogs/users/34';
www.example.com/blog/Joe
• MVC PHP framework
• Created by Taylor Otwell
• Laravel provides 20 built in library and modules
• It has various features to test modules
• Flexible Routing
• Configuration management
• Email Class
• Authentication
Laravel over CodeIgniter
• Inbuilt user authentication
• Inbuilt CLI support
• Log Management
• Template Engine
• ORM – Object Relational mapping
• MySQL is a database system used on the web
• MySQL is a database system that runs on a server
• MySQL is ideal for both small and large applications
• MySQL is very fast, reliable, and easy to use
• MySQL uses standard SQL
• MySQL compiles on a number of platforms
• MySQL is free to download and use
• MySQL is named after co-founder Monty Widenius's daughter: My
phpMyAdmin is a free software tool written in PHP, intended to handle the
administration of MySQL over the Web.
3 ways to connect to and disconnect from Database :-
• MySQLi (object-oriented) $conn->close();
• MySQLi (procedural) mysqli_close($conn);
• PDO $conn = null;

$conn = new mysqli($servername, $username, $password);


$conn = mysqli_connect($servername, $username, $password);
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username,
$password);
$result = $conn->query($sql);

if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
action on $row[“User”]…
}
}

S-ar putea să vă placă și