Professional Documents
Culture Documents
PHP INTRODUCTION
PHP stands for Personal Home Page, Hypertext
Preprocessor.
PHP is used to develop dynamic web
applications
PHP is server side scripting language
implemented by Rasmus Lerdorf in 1995 using
C and Perl technologies.
Features of PHP
Cross platform: It can run under any type of
operating system.
Cross server: It can run under different types
of web servers, like IIS, Apache , Tomcat etc.
Cross database: It supports any type of
database server.
PHP 5.0 MySQL: php 5.0 is providing MySQL
library to interact with MySQL Database.
PHP is a open source.
Features cont.
Php provides number of security functions to
apply security to applications(authentication).
Php supports few object oriented concepts
also.
Php supports different types of editors to
develop programs like notepad, edit+ etc.
Zend organization provides complete support
to php developers through online.
Php is easy to understand and easy to
develop.
WAMP
LAMP
XAMPP
After installation of xampp we can find xampp
folder in destination location.it contains number of
subfolders
htdocs
tmp
php
apache
mysql
2.Php
2.Php Short
Short open
open tag:
tag:
<?
<? ..
.. ?>
?>
3.Asp
3.Asp style
style tag:
tag:
<%
<% .
. %>
%>
4.HTML
4.HTML style
style tag:
tag:
<script
<script language=php>
language=php>
</script>
</script>
Local variables
Variable declaration within in the
function comes under local variable
declation.
We can access within the function
where we declared.
We can not access outside the
function.
<html>
<body>
<?php
function fun1()
{
$x = 200;
print $x;
}
function fun2()
{
$y = 100;
echo $y;
}
fun1();
fun2();
?>
</body>
</html>
Global Variable
Global variables we can access from
any function within the script.
By default we can not access global
variables from the function directly.
If you want to access global variables
use the keyword $GLOBALS
Example:
<?php
$sno=100;
function f1()
{
$sno=111;
echo $GLOBALS[sno];
echo $sno;
}
function f2()
{
global $sno;
$sno=200;
echo $sno;
}
fun1();
fun2();
?>
<?php
//boolean
$validUser = true;
//integer
$size = 15;
//floating point
$temp = 98.6;
//string
$cat = php programming;
//null
$here = null;
?>
Types of operators
Operator is a decision maker in php
- simple as addition/subtraction
- complex as and/or conditional statement
Types of operators
- assignment operator (=)
i.e. $var=value;
- arithmatic operators (+,_,*,/,%)
i.e. $var1=1+2;
$var2=10-3;
$var3=12*4;
$var4=12/4;
- modulus is the remainder of a division operator
i.e. 10/3=3
10%3=1
- concatination operator ( . )
i.e. $var1=$var1. red;
OR $var1.=red;
- increment (++) and decrement ( -- )
operators.
i.e. $a++; $b--;
- placement of increment and decrement
operators are important.
- prefixing ( ++$a; ) will
increment/decrement and return the value.
- Postfixing ( $a++; ) will return the value,
and the increment/decrement.
example
<?php
$a=2;
print ++$a;
print "<br>";
print $a++."--".$a;
print "<br>";
$b=5;
print $b++;
?>
Comparison operators:
- assist in the decision making process of
conditional statements
- result in a boolean value
i.e. Yes or no, 1 or 0
- 6 main operators ( ==,!=,<,>,<=,>= )
examples:
( == ) - 1==1 is true, 1==2 is false
( != ) - 1!=1 is false, 1!=2 is true
( < ) - 1<1 is false, 1<2 is true
( <= ) - 1<=1 is true, 1<=2 is true
1<=0 is false
( > ) - 1>1 is false, 1>0 is true
( >= ) - 1>=1 is true, 1>=2 is false
Logical operators
Often used in conjunction with logical operators.
- logical operators ( &&,||,!,xor )
- ( and, && ) returns true if left and right sides
are not false.
- ( or, || ) returns true if left or right sides are
true.
- ( ! ) exclamation sign modifies a true, false
statement.
- ( xor ) returns true of either the left or right are
true.
example
$a=4;
$b=6;
$c=$a+$b;
$d=$a;
$a && $b !=5;
$a || $b ==$d;
$a + $b == $c;
Logi.php
<?php
$a=4;
$b=6;
$c=$a+$b;
$d=$a;
if($a&&$b!=5)
//if($a<5 && $b>5)
{
print "welcome";
echo "hello friends";
}
?>
Arrays
Array is a collection of
heterogeneous data types.
Php is loosely typed language thats
why we can store any type of values
in arrays.
Array contains number of elements ,
each element is a combination of
element
key and element value.
Syntax: variablename=array(ele1,ele2,ele3,
);
Ex:
$arr=array(10,20,30);
Example-1
<?php
$arr=array(10,20,30);
print_r($arr);
echo $arr[2];
print "<br>";
$arr1=array(40,'scott',70);
print_r($arr1);
print "<br>";
$arr2=array(0=>45,1=>'venkat',2=>67);
print_r($arr2);
?>
<?php
$arr=array();
$arr[0]=20;
$arr[1]=40;
$arr[2]='manager';
print_r($arr);
print "<br>";
//associative array
$arr1=array();
$arr1['lang']='php';
$arr1['manager']='scot';
$arr1['sex']='male';
print_r($arr1);
//counts number of elements
echo count($arr);
?>
<?php
$arr=array(10,5,20,30);
sort($arr);//ascending order with new keys
print_r($arr);
print "<br>";
rsort($arr);//descending order with new keys
print_r($arr);
print "<br>";
asort($arr);//ascending order with original keys
print_r($arr);
print "<br>";
echo array_sum($arr);//sum of array elements
print "<br>";
echo array_product($arr);//product of array elements
print "<br>";
echo array_push($arr,40);
print "<br>";
print_r($arr);
print "<br>";
echo array_pop($arr);
print "<br>";
echo array_shift($arr);
print "<br>";
echo array_unshift($arr,5);
?>
explode(),implode()
explode()-Splits up a string by a
specified delimiter and creates an
array of strings.
implode()- Creates a string by gluing
Example:
together
array elements
a specific
$stats_array = array('name',
'ssn', 'phone'); by
// implode()
creates
a string from
separator.
an array
$stats_string = implode(",", $array);
<?php
$colors="red green orange blue";// Create a string
echo $colors;
echo "<b>\$colors is a ". gettype($colors)."\n";
$colors=explode(" ",$colors);// Split up the string by spaces
print_r($colors);
?>
array_count_values() Returns an
array consisting of the values of an
array and the number of times each
value occurs in an array.
<?php
$colors=array("red", "blue", "green", "red", "yellow",
"red","blue");
$unique_count = array_count_values($colors);
print_r($unique_count)."<br />";
?>
Strings
PHP provides many functions with which you can format and
manipulate strings.
Formatting Strings with PHP
Specifier Description
d Display argument as a decimal number
b Display an integer as a binary number
c Display an integer as ASCII equivalent
f Display an integer as a floating-point number (double)
o Display an integer as an octal number
S Display argument as a string
x Display an integer as a lowercase hexadecimal number
X Display an integer as an uppercase hexadecimal number
Example:str1.php(htdocs)
<?php
$number = 543;
printf('Decimal: %d<br/>', $number);
printf('Binary: %b<br/>', $number);
printf('Double: %f<br/>', $number);
printf('Octal: %o<br/>', $number);
printf('String: %s<br/>', $number);
printf('Hex (lower): %x<br/>', $number);
printf('Hex (upper): %X<br/>', $number);
?>
date(format, timestamp);
format- format in which the date
string to be displayed.
timestamp optional, it displays
current system date and time by
default.
Control structures
The if Statement
if (expression) {
// code to execute if the expression
evaluates to true
}
<?php
$mood = 'happy';
if ($mood == 'happy') {
echo "Hooray! Im in a good mood!";
}
?>
example
<?php
$mood='happy';
switch($mood)
{
case 'happy':
echo 'Hooray! Im in a good mood!';
break;
case 'sad':
echo 'Awww. Dont be down!';
break;
default:
echo 'Im neither happy nor sad, but $mood.';
break;
}
?>
Loops
The while Statement
Syntax: while (expression) {
// do something
}
Example:
<?php
$counter = 1;
while ($counter <= 12) {
echo $counter.'times 2 is '.($counter * 2).'<br
/>';
$counter++;
}
?>
$n=1;
do{
$n++;
echo $n;
echo "the number is" . $n . "<br>";
}while($n<5);
?>
<?php
$arr=array("welcome to","hello friends","web
programming");
foreach($arr as $value)
{
echo "processing".$value."<br>";
}
?>
<?php
$arr=array("name"=>"venkateswara
rao","age"=>31,"address"=>"hyderabad");
foreach($arr as $key=>$value)
{
echo $key. " is " . $value."<br>";
}
?>
Connecting to database(MySql)
Php is providing php_mysql.dll library with
number of functionalities to connect with
mysql databse.
Mysql is open source RDBMS. It supports
number of objects like tables,views, etc.
The default username for mysql db is root
and it does not contain any password.
We can connect to mysql db through a
command prompt by executing mysql.exe file
c:/xampp/mysql/bin>mysql.exe
mysql>create database dbname;
mysql>use dbname;
phpmyadmin
It is a GUI used to connect with mysql db.
It is available with xampp download.
The url address to open phpmyadmin is
http://localhost/phpmyadmin.
insert we can insert records in a table
browse we can browse the table records
structure to change the structure of a table
sql we can execute our sql statements
export we can export database tables into
text files,pdf,excell,etc.
import we can import the exported file
empty we can delete the table records
drop we can delete the table structure
servername
Username
password
mysql_select_db:
to select database from mysql server,arguments are sql statement
and connection id.
mysql_query:
To execute sql query in mysql database. arguments are sql
statement and connection id.
mysql_error:
To get the error messages while executing mysql statements.
mysql_errno:
To get the error number while executing mysql statements.
Creating database
<?php
$con=mysql_connect("localhost","root");
if(mysql_query("create database venkat",$con))
echo "database created";
else
echo mysql_error();
?>
Creating table
<?php
$con=mysql_connect("localhost","root");
mysql_select_db(db_new,$con);
if(mysql_query("create table customer(sno int)",
$con))
echo table created";
else
echo mysql_error();
?>
Example:loginmysql.html
insert.php
PHP - Sessions
<?php
session_start();
if( isset( $_SESSION['counter'] ) )
{
$_SESSION['counter'] += 1;
}
else {
$_SESSION['counter'] = 1;
}
$msg = "You have visited this page ".
$_SESSION['counter'];
$msg .= "in this session."; ?>
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
sessions
Server system
unsecured
secured