Learn PHP On Single Page
PHP
3 letters together constitute the world most popular programming languages for web development .
PHP – Hypertext Preprocessor
Means that : it preprocess the hypertext
PHP – Hypertext Preprocessor
Means that : it preprocess the hypertext
- · Inbuilted Functionality
- · Security
- · Supports different database and data formats
Features of PHP:
Ok Let we start with PHP:
Run Php
· Install WAMP server-----for windows.
· (or) install XAMP server----for Windows.
· Install LAMP for LINUX machines.
· Eg: let as consider WAMP sever ,for WAMP the default host name is “localhost”.
· Find the www folder C:/ (or) where you installed the WAMP and create a new folder.
· The folder name should be without any space .
· Place your php file inside the folder.
· Every php file must have the file extension of (.php).
· Then open your browser and type your hostname (eg. Localhost).
· The wamp page will show in that find your php folder under projects and open your folder , then your php file.
Basics Of Php
Php file should have the extension of .php, Use text editor,notepad,dreamweaver,sublime editor,notepad++, and other editors to create and edit the php files
PHP TAGS
<?php
-----code----
?>
Eg: <?php ------php codes here-----?>
Define The Php Variable
Every PHP variable must be defined with the symbol called “$”
Eg: <?php $variable ?>
Assign Initialize A Variable
<?php
$variable=”one”;
?>
// every php code line will ends with the semi colen (;)
Assign Value To Multiple variables
<?php
$variable_one=”first”;
$variable_two= $variable_one;
?>
//$variable_two value will be first
Php Datatypes
Examples | Name of the Datatype |
$validity=true; | Boolean |
$size=15; | Integer |
$temp=4.44; | Float |
$cat=”animal”; | String |
$var=null; | Null string |
Php Comment Line
Comment line is used to comment between the codings for our knowledge to know some additional informations
In php 2 symbols are used to give comment
They are
//comment line
#comment line
Eg:
<?php
//this is php file
//this line doesn’t print while running
#thank you
?>
Print Php Variables
<?php
$string =”Hello World!!”;
echo $string;
?>
//php is case sensitive so give your variables carefully
//give echo to print the variable values
OUTPUT: Hello World!!
<?php
$string =”Hello World!!”;
print $string;
?>
//php is case sensitive so give your variables carefully
//use print keyword to show the value
OUTPUT: Hello World!!
//first you have to initialize the variable
<?php
$string =””;
//also give $string =null;
echo $string;
?>
OUTPUT:
Unset a Variable
<?php
$var =’print the character’;
Echo $var; //output is print the character
Unset($var);
Echo $var;//output is --------null----------
?>
Print String along with PHP variables
<?php
$name=’Arun yokesh’;
Echo “my name is”.$name;
?>
//To join php variable with other thing use dot(.)
OUTPUT: my name is Arun yokesh
. is used to attach a php variable along with the sentence
<?php
$price=500;
$discount=200;
Echo “brand new pencil is Rs”.$price.”after discount”.$discount.”rupees”;
?>
OUTPUT: brand new pencil is Rs 500 after discount 200 rupees
STRING CONCADINATION
<?php
$numbers = “”;
$one=1;
$two=2;
$three=3;
$four=4;
$numbers .=$one.$two.$three.$four;
Echo $numbers;
?>
OUTPUT: 1234
<?php
$numbers = “”;
$one=1;
$two=2;
$three=3;
$numbers .=$one;
$numbers .=$two;
$numbers .=$three;
Echo $numbers;
?>
OUTPUT: 123
// to join text with old stored data used . before “=” operator
PHP Operators
Operators | Meanings |
== | Equal to |
!= | Not equal to |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
=== | Equal to and of the same type |
PHP loop operators
Operators | Meanings |
&& | AND |
|| | OR |
! | NOT |
Php arrays
<?php
$fruits=array(‘apple’,’banana’,’grape’);
Echo $fruits[1];
?>
OUTPUT: banana
<?php
$fruits = array(
‘a’ => ‘apple’,
‘b’ =>’banana’
);
Echo $fruits[‘a’];
?>
OUTPUT: apple
<?php
$data = array(
‘username’ => ‘arun’,
‘password’ => ‘secret’,
‘host’ => ‘192.168.1.4’
);
Echo $data[‘host’];
?>
OUTPUT: 192.168.1.4
To get the array size use
<?php
$size= count($data);
Echo $size;
?>
//no of values stored
Get array values
<?php
$data_store = array(‘one’,’two’,’three’);
Echo $datastore[1];
?>
OUTPUT: two
<?php
Foreach($data_store as $i)
{
Echo $i “\n”;
}
?>
\n is used to produce a new line
OUTPUT:
One
Two
Three
PHP statements and conditions
Break; |
Continue; |
Exit; |
If conditions
<?php
If(condition)
{
-----code-----
}
?>
If else statement
<?php
If(condition)
{
-----code 1-----
}
Else
{
-----code 2---
}
?>
Switch case statement
<?php
$today=’monday’;
Switch($today)
{
Case ‘monday’:
Echo ‘1’;
Break;
Case ‘tuesday’:
Echo ‘2’;
Break;
Case ‘wednesday’:
Echo ‘3’;
Break;
Case ‘thursday’:
Echo ‘5’;
Break;
Case ‘friday’:
Echo ‘6’;
Break;
Case ‘saturday’:
Echo ‘7’;
Break;
?>
OUTPUT : 1
PHP loops
While loop
<?php
While(condition)
{
-----code----
}
?>
Do while loop
<?php
Do
{
-----code-----
}
While(condition)
?>
For loop
<?php
For(initialization;condition;increment/decrement)
{
-------code-----
}
?>
Ex:
<?php
For($i=0;i<=3;i++)
{
Echo $i;
}
?>
OUTPUT : 0123
PHP functions
<?php
Function function_name()
{
--------Execution code----
}
Function_name();
// call function
Ex:
<?php
function print($str) // argument passing in the function
{
$var = $str;
Echo $var;
}
Print(‘pondicherry’);
?>
OUTPUT : Pondicherry
Create Database with PHP MY ADMIN
· Type localhost on browser and select the php my admin link
· Click create database
· Enter the name for your new database
· Create a new table in that database
· Enter the table name and number of fields you want
· Basically column datatypes wil be int or varchar
· Enter the table fields along with the datatypes and link save
· If you know sql queries u directly click the sql tab at the top and type the sql query
· The default user name and password the database is
· Username = root
· Password=””
Database Connectivity
<?php
Mysql_connect_db(“---hostname---“,”------database username----“,”-----database password----“);
Mysql_select_db(“---database name—“);
?>
Ex:
<?php
Mysql_connect_db(‘localhost’,’root’,’’);
$connect = Mysql_select_db(‘test_db’);
If($connect)
{
Echo “ db connected”;
}
Else
{
Echo “unable to connect db”;
}
?>
PHP date and time
<?php
$today= date(“Y-m-d”); // 2014-01-04
$time = date(“h-i-s”); // 12-04-28 ---- hours-mint-sec
$time=time(); //default system time
$range = date(“ h-i-s-y-m-d”); /// 08-43-56-14-01-04 hour-min-sec-yr-month-date
?>
PHP set and unset session
Session is mainly used for security
Before set and unset the session you have to start the session
<?php
Session_start();
$_SESSION[‘username’] = ‘yokesh’;
?>
Mainly session is used to avoid illegal access of php pages
<?php
Session_start();
$_SESSION[‘username’] = ‘yokesh’;
Echo $_SESSION[‘username’];
?>
OUTPUT: yokesh
Unset the session
<?php
Unset($_SESSION[‘username’]);
//or
$_SESSION[‘username’]=””;
?>
PHP database insert
Table structure
Username | Password |
Insert.html
<form action=’insert.php’ method=’post’ >
<label>username</label>
<input type=’text’ name=’username’>
<label>password</label>
<input type=’text’ name=’password’>
<input type=’submit’ name=’submit’ value=’submit’>
</form>
db_connect.php
<?php
Mysql_connect_db(‘localhost’,’root’,’’) or die();
Mysql_select_db(“user_db”)or die();
?>
Insert.php
<?php
Include “dbconnect.php”; //including the db_connect file with this file
If($_POST) /// if post method came
{
If($_POST[‘submit’]) // if post method came with the name of submit
{
$name= $_POST[‘username’];
$pass=$_POST[‘password’];
$query = mysql_query(“INSERT INTO user_table (username,password) VALUES (‘$name’,’$pass’)”; // user_table is the the db table name
If($query)
{
Echo “data inserted”;
}
Else
{
Echo “error in data insertion”;
}
}
}
?>
PHP database delete
Table structure
Username | Password |
Arun | 123 |
Kumar | kumar |
Delete.php
<?php
Include “db_connect.php”;
//used to include that file with this current file
//u may also use require function
$delete_query = mysql_query(“delete from user_table where username=’arun ’ “);
If($delete_query)
{
Echo “record deleted”;
}
Else
{
Echo “error occurred in deletion”;
}
?>
After deletion the table will be
Table structure
Username | Password |
Kumar | Kumar |
PHP Database Select / Display
Table structure
Username | Password |
Arun | 123 |
Kumar | kumar |
<?php
Include “db_connect.php”;
$query = mysql_query(“select * from user_table”); // * means all contents
While($row = mysql_fetch_array($query)) // while is used to fetch each row
{
Echo $row[‘username’]; // db column name
Echo<”<br/>”; //give line space while printing
Echo $row[‘password’];
Echo<”<br/>”; //give line space while printing
} // it will display all contents in the user_table
?>
// query for selected columns
Select username from user_table //for only username
Select password from user_table // for only password colum
OUTPUT :
Arun
123
Kumar
Kumar
PHP database Update
Table structure
Username | Password |
Arun | 123 |
Kumar | kumar |
Yokesh | yokesh |
<?php
Include “db_connect.php”;
$query=mysql_query(“update user_table set password=’abc’ where username =’kumar’”);
// where is used to give the condition in query
If($query) // if done
{
Echo “data updated”;
}
Else
{
Echo “error occurred in updation”;
}
?>
After updation the db table will be
Table structure
Username | Password |
Arun | 123 |
Kumar | abc |
Yokesh | yokesh |
Conclusion
Thank you!!
Contact me :
Email : ayokesh.cse@gmail.com
Facebook : www.facebook.com/ayokesh
Reference
Ø PHP & MYSQL for dummies
Ø My firm
Ø PHP.net ( websites)
Ø W3schools.com
Ø PHP 6 A beginner’s Guide