7).PHP-1 - WordPress.com

advertisement
LAMP Architecture



Typical PHP and MYSQL application
development framework follows “LAMP “
Architecture
“LAMP”(Linux , Apache , MySQL and PHP)
platform wherein each component plays an
important role.
LINUX provides the base OS and Server
Environment
LAMP Architecture



The Apache Web Server intercepts HTTP
requests and either serves them directly or
passes them onto the PHP interpreter for
execution
The PHP interpreter parses and executes
PHP code & returns the results to the
webserver
MYSQL RDMS serves as the data storage
engine , built connections from PHP Layer for
inserting , modifying or retrieving data.
Learning PHP
Learning Basics
Embedding PHP in HTML
There are three possibilities

<?php
….PHP code …
?>

<?
…..PHP Code …..
?>

<%
…..PHP Code….
%>

<script language = “php”>
……PHP Code
</ script >

Sending output to Browser
Echo
<?php
echo “Hello From PHP “ ;
?>
Echo is not a function but a language Construct which sends a
String value to the Browser
How PHP Works
 ends with ;
 Code blocks are enclosed in { }
 Comments begin with // or # (Single line) and /* …. */ (For Multi
Line)

To run a Simple PHP File



Make sure that PHP and IIS is installed and
properly running
Save Every PHP code with .php extension in
directory
c:/intepub/wwwroot/php/filename.php
To access the file type in the url bar of IE
http://localhost/php/filename.php
Language Basics






Variable Names
Begin with a $ sign
First Letter after the $ must be a letter or an
underscore
Remaining letters may be numbers or
underscores w/o a fixed limit
Case-sensitive
Longer than 30 characters impractical
Data Types



PHP is loosely typed language don’t requires
declaration of a variable type and
automatically convert variable type
depending upon the context in which they are
used and operations performed on their
values
Enables you to check the data type whenever
you want to
Supports following data types
Data Types








Boolean
Integer
Float
String
Array
Object
Resource
NULL
Assigning & Using Variable Values


<?php
$age = $dob +15;
?>
<?php
$today = “Jan 05 2004”;
echo “today is $today “ ;
?>
Detecting the Data type
Use of gettype() function
<?php
$auth = true;
$age = 27 ;
$name = ‘Bobby’;
$temp = 98.6;
//returns String
echo gettype($name) ;

//returns String
echo gettype($name) ;
//returns Boolean
echo gettype($auth) ;
//returns integer
echo gettype($age) ;
//returns double
echo gettype($temp) ;
?>
Detecting the Data type









Specialized functions
is_bool
is_string
is_numeric
is_float
is_int
is_null
is_array
is_object
NULL Values

A Null is typically seen when a variable is initialized but not assigned a
value
<?php
//checks type of un initialized variable
//returns NULL
echo gettype($me);
$me = “Mysterious”;
//Check type again
//returns STRING
echo gettype($me);
Unset($me);
//returns NULL
echo gettype($me);
?>
Using Operators

<?php
$num1 = 101;
$num2 = 5;
$sum = $num1 + $num2;
$diff = $num1 - $num2;
$product = $num1 * $num2;
$quotient = $num1 / num2 ;
$remainder = $num1 % $num2 ;
?>
Using Operators
Operators
What It does
.
Concatenation
==
Equal to
===
Equal to and of the same type
!==
Not Equal to or not of the same type
<> aka !=
Not Equal to
&&
Logical AND
||
Logical OR
XOR
Logical XOR
!
Logical NOT
++
Increment
--
Decrement
The === Operator
<?php
//define two variables
$str = ’14’;
$int = 14;
//returns true b/c contains the same value
$result = ($str == $int );
//returns false b/c variables are not of the same types
$result = ($str === $int );
?>
if() Statement
<?php
if (conditional test)
{
do this;
}
else
{
do this;
}
?>
If elseif
PHP also provides if-elseif-else() construct
<?php
if (conditional test)
{
do this;
}
elseif (conditional test #1)
{
do this;
}
………..
elseif (conditional test #N)
{
do this;
}
else
{
do this ;
}
?>
switch() Statement
<?php
switch ($country)
{
case ‘UK’ :
$capital = ‘London’;
break;
case ‘US’ :
$capital = ‘Washington’;
break;
case ‘FR’ :
$capital = ‘Paris’;
break;
default :
$capital = ‘Unknown’;
break;
}
?>
Other Constructs
 Ternary Operator
$msg = $dialCount > 10 ? “Cannot connect after 10 attempts “ : “Dialing” ;
while() Loop
while( condition is true)
{ do this; }
 do() Loop
do
{
dothis;

} while (condition is true)
for() Loop
<?php
for ($x = 0 ; $x <= 100 ; $x++)
{
echo “$x”;
}
?>
Using Arrays
Array is a complex variable that allows to
store multiple values in a single variable
 Can be thought of as a “container” variable
which can contain one or more values
<?php
$flavors = array(‘Strawberry’, ‘Grape’ , “Vanilla”
,’Caramel’, “Choclate” );
?>

Using Arrays



Elements are accessed via index numbers
the first index starts at 0
PHP enables you to replace indices with user
defined “ keys” to create a slightly different
type of array . Each key is unique and
corresponds to a single value.
Keys may be made up of any string
characters
Using Arrays



$Fruits = array("RED"=> "APPLE" ,
"YELLOW" =>"BANANA" ,"PURPLE" =>
"PLUM" , "GREEN"=>"GRAPE");
This is an array variable containing 4 key
value pairs
They are referred to as Hash or Associative
Array
Creating Arrays
<?php
$flavors[0] = “Strawberry”;
$flavors[1] = “Grapes”;
OR For Associative Array
$Fruits[red] = “Apple”;
$Fruits[green] = “Grape”;
Modifying Array Elements


To Add an element to an Array , assign a
value using the next available index number
or key
$flavors[5] = “mango”;
OR
$flavors [] = “mango”;
To modify an element of an array , assign a
new value to the corresponding scalar
variable .
$flavors[0] = “Blueberry”;
Processing Arrays with Loops
<?php
$flavors = array("Strawberry", "Grape" ,
"Vanilla" ,"Caramel", "Choclate" );
for($x=0; $x < sizeof($flavors); $x++)
{
echo “<li> $flavors[$x] “ ;
}
?>
The foreach() Loop



A new loop type introduced in PHP4.0 for the
purpose of iterating over an array
This loop runs once for each element of the
array
Unlike a for loop a foreach() loop does not
need a counter or a call to sizeof(); it keeps
track of it’s position in the array automatically
The foreach() Loop
<?php
$num = array(10,20,30,40,50,60,70);
foreach($num as $item)
{
echo "<li> $item ";
}
?>
Download