php basic tutorial new
TRANSCRIPT
-
8/8/2019 Php Basic Tutorial New
1/60
www.virtual-solutions.in
PHP BASIC TUTORIAL
ARCHITECTURE
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 1
-
8/8/2019 Php Basic Tutorial New
2/60
www.virtual-solutions.in
WAMP
WAMPs are packages of independently-created programs installed on computers
that use a Microsoft Windows operating system.
WAMP is an acronym formed from the initials of the operating system Microsoft
Windows and the principal components of the package:
Apache, MySQL and PHP (or Perl or Python, although WAMP includes PHP
exclusively). Apache is a web server. MySQL is an open-source database. PHP is a
scripting language that can manipulate information held in a database and generate
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 2
-
8/8/2019 Php Basic Tutorial New
3/60
www.virtual-solutions.in
web pages dynamically each time content is requested by a browser. Other programs
may also be included in a package, such as phpMyAdmin which provides a graphical
user interface for the MySQL database manager, or the alternative scriptinglanguages Python or Perl.
WAMP is an integrated environment where we can use PHP, MySQL and Apache
webserver in Windows Operating System
Wamp Installation
Download latest version of WAMP from http://www.wampserver.com/en/.
The following screen schots are self-explanatory in the process of installing
the WAMP server in a Windows computer.
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 3
-
8/8/2019 Php Basic Tutorial New
4/60
www.virtual-solutions.in
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 4
-
8/8/2019 Php Basic Tutorial New
5/60
www.virtual-solutions.in
After installing you can load the WAMP server and on loading it, you can find theWAMP tray in the taskbar at the right end. For the WAMP to work the Server has to
be put online and Start All The Services.
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 5
-
8/8/2019 Php Basic Tutorial New
6/60
www.virtual-solutions.in
If everything is working fine, the below given screen or the index.php file should be
displayed when localhost// is entered in any browser.
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 6
-
8/8/2019 Php Basic Tutorial New
7/60
www.virtual-solutions.in
The below given screenshots are self-explanatory for how to use phpmyadmin and tocreate database, tables, enter values in a table and related stuffs.
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 7
-
8/8/2019 Php Basic Tutorial New
8/60
www.virtual-solutions.in
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 8
-
8/8/2019 Php Basic Tutorial New
9/60
www.virtual-solutions.in
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 9
-
8/8/2019 Php Basic Tutorial New
10/60
www.virtual-solutions.in
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 10
-
8/8/2019 Php Basic Tutorial New
11/60
www.virtual-solutions.in
Basics of PHP
Php Syntax
(or)
Print statement
Variables
There is no need to explicitly declare variables in php. The first value whichyou assign to the variable will decide the variable datatype.
-
8/8/2019 Php Basic Tutorial New
12/60
www.virtual-solutions.in
echo $x;?>
The Concatenation Operator
There is only one string operator in PHP.
The concatenation operator (.) is used to put two string valuestogether.
The strlen() function
The strlen() function is used to return the length of a string.
The strpos() function
The strpos() function is used to search for characterwithin a string.
If a match is found, this function will return the position of the first
match. If no match is found, it will return FALSE.
Assignment Operators
Operator Example
= X=y
+= x+=y
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 12
-
8/8/2019 Php Basic Tutorial New
13/60
www.virtual-solutions.in
-= X-=y
*= x*=y
/= x/=y
.= x.=y
%= x%=y
Comparison Operators
Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
is not equal 58 returns true
> is greater than 5>8 returns false
< is lesser than 5= is greater than or equal to 5>=8 returns false
-
8/8/2019 Php Basic Tutorial New
14/60
www.virtual-solutions.in
Control Statements
if statements
if (condition) code to be executed if condition is true;
Sample code:
if...else Statements
if (condition) code to be executed if condition is true;else
code to be executed if condition is false;
Sample code:
if...elseif....else Statements
if (condition)code to be executed if condition is true;
elseif (condition) code to be executed if condition is true;
else code to be executed if condition is false;
Sample code:
-
8/8/2019 Php Basic Tutorial New
15/60
www.virtual-solutions.in
echo "Have a nice Sunday!";
elseecho "Have a nice day!";
?>
Switch Statements
switch (n)
{case label1: code to be executed if n=label1;break;
case label2: code to be executed if n=label2;break;
default: code to be executed if n is different from both label1 and label2;}
Sample code:
Array
A variable is a storage area holding a number or text. The problem is, avariable will hold only one value.
An array is a special variable, which can store multiple values in onesingle variable.
There are three kind ofarrays
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 15
-
8/8/2019 Php Basic Tutorial New
16/60
www.virtual-solutions.in
Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with
a value
Multidimensional array - An array containing one or more arrays
Numeric Arrays
There are two methods to create a numeric array.
o $cars=array("Saab","Volvo","BMW","Toyota");
o $cars[0]="Saab";$cars[1]="Volvo";$cars[2]="BMW";
$cars[3]="Toyota";
Sample code:
Associative Arrays
An associative array, each ID key is associated with a value.
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 valuesto them.
There are two methods to create a numeric array
o$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
o $ages['Peter'] = "32";$ages['Quagmire'] = "30";$ages['Joe'] = "34";
Sample code:
-
8/8/2019 Php Basic Tutorial New
17/60
www.virtual-solutions.in
$ages['Quagmire'] = "30";$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";?>
Multidimensional Arrays
In a multidimensional array, each element in the main array can alsobe an array. And each element in the sub-array can be an array,and so on.
Sample code:
PHP Looping
While Loops
while (condition){code to be executed;}
While Loop:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 17
-
8/8/2019 Php Basic Tutorial New
18/60
www.virtual-solutions.in
Do...while Statements
do{
code to be executed; }while (condition);
Sample code:
The for Loops
for (init; condition; increment){ code to be executed;}
Sample code:
The foreach Loop
foreach ($arrayas$value){
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 18
-
8/8/2019 Php Basic Tutorial New
19/60
www.virtual-solutions.in
code to be executed;}
Sample code:
PHP Function
function functionName(){code to be executed;
}
Method 1 Sample code:
Method 2 Sample code:
-
8/8/2019 Php Basic Tutorial New
20/60
www.virtual-solutions.in
echo $fname . " Refsnes" . $punctuation . "
";}echo "My name is ";writeName("Kai Jim",".");
echo "My sister's name is ";writeName("Hege","!");
echo "My brother's name is ";writeName("Stale","?");
?>
Method 4 with Return Value Sample code:
HTML FORM
Name:
Age:male
female
PHP Methods
Two types to pass the value from htmlto php are
GET method
POST method
GET METHOD:
$a=$_GET[name];
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 20
-
8/8/2019 Php Basic Tutorial New
21/60
www.virtual-solutions.in
where name is value from the user in get method.
POST METHOD:
$a=$_POST[name];
where name is value from the user in post method.
DIFFERENCE BETWEEN GET AND POST
When using GET method the values which are passed can be seen in
address bar.
But these values cannot be seen in address bar in POST method.
Basics of HTML FORMS
HTML FORM TEXT BOX
Name:
Age:welcome.php
Form output:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 21
-
8/8/2019 Php Basic Tutorial New
22/60
www.virtual-solutions.in
MENU/LIST
The tag is used to create a select list (drop-down list).
The tags inside the select element define the available optionsin the list.
Sample Code
Volvo
Saab
MercedesAudi
Output:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 22
-
8/8/2019 Php Basic Tutorial New
23/60
www.virtual-solutions.in
MenuSample Code
Emo
Metal/RockHip Hop
Ska
JazzCountry
ClassicalAlternative
Oldies
Techno
Output
Radio
defines a radio button. Radio buttons let a user select ONLY ONE one of a
limited number of choices
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 23
-
8/8/2019 Php Basic Tutorial New
24/60
-
8/8/2019 Php Basic Tutorial New
25/60
www.virtual-solutions.in
Comments
The tag defines a multi-line text input control.
Sample Code
At W3Schools you will find all the Web-building tutorials you need, from basic HTML
to advanced XML, SQL, ASP, and PHP.
Output
Pictures
Can Upload Images using multipart/form-data
Sample Code:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 25
-
8/8/2019 Php Basic Tutorial New
26/60
www.virtual-solutions.in
Output
Check Box
A small box on which an x or check mark appears when the option.
defines a checkbox. Checkboxes let a user select
ONE or MORE options of a limited number of choices.
Sample Code
I have a bike
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 26
-
8/8/2019 Php Basic Tutorial New
27/60
www.virtual-solutions.in
I have a car
Output
HTML FORM EXAMPLE:
Name
Male
Female
I have a bike
I have a car
VolvoSaabMercedesAudi
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 27
-
8/8/2019 Php Basic Tutorial New
28/60
www.virtual-solutions.in
Post.php
Output
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 28
-
8/8/2019 Php Basic Tutorial New
29/60
www.virtual-solutions.in
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 29
-
8/8/2019 Php Basic Tutorial New
30/60
www.virtual-solutions.in
PHP Date()
d - Represents the day of the month (01 to 31).
m - Represents a month (01 to 12).
Y - Represents a year (in four digits).
Sample code:
Output:
PHP include() Function
The include() function takes all the content in a specified file and includesit in the current file.
If an error occurs, the include() function generates a warning, but the
script will continue execution.
Homepage.php
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 30
-
8/8/2019 Php Basic Tutorial New
31/60
-
8/8/2019 Php Basic Tutorial New
32/60
www.virtual-solutions.in
Modes Description
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
wWrite only. Opens and clears the contents of file; or
creates a new file if it doesn't exist
w+Read/Write. Opens and clears the contents of file; or
creates a new file if it doesn't exist
a Append. Opens and writes to the end of the file orcreates a new file if it doesn't exist
a+Read/Append. Preserves file content by writing to the
end of the file
xWrite only. Creates a new file. Returns FALSE and an
error if file already exists
x+Read/Write. Creates a new file. Returns FALSE and an
error if file already exists
Closing a File
Check End-of-file
if (feof($file)) echo "End of file";
Reading a File Line by Line
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 32
-
8/8/2019 Php Basic Tutorial New
33/60
www.virtual-solutions.in
Reading a File Character by Character
Write
Syntax: $myFile = "testFile.txt"; $fh = fopen($myFile, 'w');
Sample code:
-
8/8/2019 Php Basic Tutorial New
34/60
-
8/8/2019 Php Basic Tutorial New
35/60
www.virtual-solutions.in
Cookies
A cookie is often used to identify a user. A cookie is a small file that the server
embeds on the user's computer. Each time the same computer requests a page with
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 35
-
8/8/2019 Php Basic Tutorial New
36/60
www.virtual-solutions.in
a browser, it will send the cookie too. With PHP, you can both create and retrievecookie values.
Sample code:
How to Retrieve a Cookie Value?
How to Delete a Cookie?
Sessions
A PHP session variable is used to store information about, or change settings for auser session. Session variables hold information about one single user, and areavailable to all pages in one application.
Session syntax:
Sample code:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 36
-
8/8/2019 Php Basic Tutorial New
37/60
www.virtual-solutions.in
A Basic E mail Sending
Database Programming using PHP
PHP DATABASE mysql
MySQL is a database.
The data in MySQL is stored in database objects called tables.
A table is a collection of related data entries and it consists of columns
and rows.
Connection to a MySQL Database:
Syntax: mysql_connect(servername,username,password);
Opening connection:
-
8/8/2019 Php Basic Tutorial New
38/60
www.virtual-solutions.in
// some code?>
Closing connection:
Create a Database:
Syntax: CREATE DATABASE database_name
Sample code:
Create a Database output:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 38
-
8/8/2019 Php Basic Tutorial New
39/60
www.virtual-solutions.in
Create a Table
Syntax: CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,column_name3 data_type,....
)
Sample code:
$con = mysql_connect("localhost","root","");
-
8/8/2019 Php Basic Tutorial New
40/60
www.virtual-solutions.in
)";echo "TABLES CREATED";
// Execute querymysql_query($sql,$con);
mysql_close($con);?>
Table output:
Insert Data Into a Database Table
INSERT INTO table_name
VALUES (value1, value2, value3,...)
INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)
Sample code:
-
8/8/2019 Php Basic Tutorial New
41/60
-
8/8/2019 Php Basic Tutorial New
42/60
www.virtual-solutions.in
if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());}
echo "1 record added";
mysql_close($con)?>
Insert from user output:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 42
-
8/8/2019 Php Basic Tutorial New
43/60
www.virtual-solutions.in
Select Data From a Database Table
Syntax: SELECT column_name(s)FROM table_name
Sample code:
Select Data output:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 43
-
8/8/2019 Php Basic Tutorial New
44/60
www.virtual-solutions.in
The WHERE clause
Syntax: SELECT column_name(s)FROM table_nameWHERE column_name operator value
Sample code:
-
8/8/2019 Php Basic Tutorial New
45/60
www.virtual-solutions.in
Update Data In a Database
Syntax: UPDATE table_nameSET column1=value, column2=value2,...
WHERE some_column=some_value
Sample code:
-
8/8/2019 Php Basic Tutorial New
46/60
www.virtual-solutions.in
Delete Data In a Database
Syntax: DELETE FROM table_name WHEREsome_column = some_value
Sample code:
-
8/8/2019 Php Basic Tutorial New
47/60
www.virtual-solutions.in
User Authentication in PHP
Here are some authentication method that we'll discuss
Basic authentication
We hard code the username and password combination in the login script
itself. Suitable for simple application
User & password stored in database
A very common method. We store all the user name and password
information in the database
User authentication with image verification
This is a more advance method of user authentication. We can prevent
any automatic login by a robot ( script ) by using this method
User Authentication
Output:
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 47
-
8/8/2019 Php Basic Tutorial New
48/60
www.virtual-solutions.in
Basic javascript
What is JavaScript?
JavaScript was designed to add interactivity to HTML pages
JavaScript is a scripting language
A scripting language is a lightweight programming language
JavaScript is usually embedded directly into HTML pages
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 48
-
8/8/2019 Php Basic Tutorial New
49/60
www.virtual-solutions.in
JavaScript is an interpreted language (means that scripts execute
without preliminary compilation)
Everyone can use JavaScript without purchasing a license
Basic javascript:
document.write("This is my first JavaScript!");
Sample code:
function valid()
var a=document.getElementById("login");var b=document.getElementById("password");
if(a.value=="" || b.value==""){
alert ("Fields are empty");}
-
8/8/2019 Php Basic Tutorial New
50/60
-
8/8/2019 Php Basic Tutorial New
51/60
www.virtual-solutions.in
CSS
CSS stands for Cascading Style Sheets.
Styles define how to display HTML elements.
Syntax:
Id and Class
The id Selector #para1
{
text-align:center;color:red;
}
Class selector
center {text-align:center;}
Three Ways to Insert CSS
External style sheet
Internal style sheet
Inline style
External Style Sheet
Internal Style Sheet
hr {color:sienna;}p {margin-left:20px;}
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 51
-
8/8/2019 Php Basic Tutorial New
52/60
www.virtual-solutions.in
body {background-image:url("images/back40.gif");}
Inline Styles
This is a paragraph.
Css properties
Styling Backgrounds
background-color
background-image
background-repeat
background-attachment
background-position
Text formatting
color
text-align
text-decoration
text-transform
text-indent
Font
font-family
font-style
font-size
Links
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 52
-
8/8/2019 Php Basic Tutorial New
53/60
www.virtual-solutions.in
a:link {color:#FF0000;} /* unvisited link
a:visited {color:#00FF00;} /* visited link
a:hover {color:#FF00FF;} /* mouse over link
a:active {color:#0000FF;} /* selected link
List
ul.a {list-style-type: circle;}
ul.b {list-style-type: square;}
ol.c {list-style-type: upper-roman;}
ol.d {list-style-type: lower-alpha;}
Where ul is unordered list and ol is ordered list.
Padding
padding-top;
padding-bottom;
padding-right;
padding-left;
padding-top:25px;
padding-bottom:25px;padding-right:50px;
padding-left:50px;
Can be written as
Padding: 25px 25px 50px 50px;
Grouping Selectors
h1{
color:green;}
h2{
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 53
-
8/8/2019 Php Basic Tutorial New
54/60
www.virtual-solutions.in
color:green;}
p{
color:green;}
Can be written as
h1,h2,p{
color:green;}
Examples of Css link:
Head Section:
Body Section
Children are the living messages we
send to a time we will not see. India, by one count, has 18 millionstreet children and over 22 million child laborers.
Millions of children go to bed eachnight, hungry and without the warmth of a home. Many have neitherbeen to school nor have access to basic healthcare.
stylesheet.css
.font{
font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;
}.font1{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;Color:#2887E1;}
PHP EDITORS
Dreamweaver
PHP designer
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 54
-
8/8/2019 Php Basic Tutorial New
55/60
www.virtual-solutions.in
Notepad
OPEN SOURCEOpen-source software (OSS) is computer software that is available in source codeform for which the source code and certain other rights normally reserved for
copyright holders are provided under a software license that permits users to study,change, and improve the software.
PHP OPEN SOURCE CATEGORIES:
1. Content Management System(CMS)
2. Learning Management System (LMS)3. Social Networking
4. Ecommerce
5. Client Relationship Management(CRM)6. Blog
Content Management System1. Drupal
2. Joomla
Learning Management System1. Moodle
Social Networking1. Elgg2. Mahara
Ecommerce1. Oscommerce
2. Magento3. Zencart
Client Relationship Management1. Vtiger
2. Sugar CRM
Blog1. Wordpress
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 55
-
8/8/2019 Php Basic Tutorial New
56/60
www.virtual-solutions.in
OOPS CONCEPT IN PHP
OOP language should have:
Abstract data types and information hiding
Inheritance
Polymorphism
CLASSES
A class is technically defined as a representation of an abstractdata type. In laymen's terms a class is a blueprint from which newwill construct our box. It's made up of variables and functionsthat allow our box to be self-aware.
Sample Code
-
8/8/2019 Php Basic Tutorial New
57/60
www.virtual-solutions.in
}
$mybox = new Box("Jack");echo $mybox->get_whats_inside();
?>
Methods
To ask the box what it contains, the special get_whats_inside function wasused. Functions defined in the class are known as methods; they act as a
method for communicating with and manipulating the data within the box.
The nice thing about methods is that they allow us to separate all the classcoding from our actual script.
We could save all of the class code in a separate file and then use include to
import it into our script. Our scripts become more streamlined and, because
we used descriptive names for our methods, anyone else reading our codecan easily see our train-of-thought.
Sample Code
Extends
We could add more methods to our class, but what if we were buildingextensions to someone else's class? We don't have to create an entirelynew class to add new functionality. we can build a small extension classbased on the original.
Sample Code
-
8/8/2019 Php Basic Tutorial New
58/60
www.virtual-solutions.in
function get_shoe_size() {return $this->size;
}
}
$mybox = new ShoeBox("Shoes", 10);echo $mybox->get_whats_inside();echo $mybox->get_shoe_size();?>
The ability to write such modular additions to your code gives greatflexibility in testing out new methods and saves time by reusing thesame core code.
Constructor
Developers can declare constructor methods for classes. In this, those classes which
have a constructor method call this method for each newly created object. So, itis suitable for any initialization that the object may need before it is used. In this
the parent constructors are not called implicitly if the child class defines aconstructor.
Example
-
8/8/2019 Php Basic Tutorial New
59/60
www.virtual-solutions.in
function __construct() {
parent::__construct();
print "In ChildClass constructor\n";
}
}
$obj = new ParentClass();
$obj = new ChildClass();
?>
Destructors
This destructor concept is similar to other object oriented languages. In thisthe destructor will be called as soon as all references to a particular object
have been removed or when the object has been explicitly destroyed.
Example
Dr.M.G.R.University PHP/MySQL Training September 13-18 2010 59
-
8/8/2019 Php Basic Tutorial New
60/60
www.virtual-solutions.in
Patterns
Patterns are ways to describe best practices and good designs. The patternsshow a flexible solution to common programming problems.
Example