Date post: | 05-Dec-2014 |
Category: |
Education |
Upload: | mandakini-kumari |
View: | 579 times |
Download: | 2 times |
MandakiniAyushi Infotech
Mandakini Kumari22nd July 2012http://www.slideshare.net/mandakinikumari
PHP Basic
Overview A Brief HistoryWhat is PHPWhy PHPPHP StackPHP SyntaxVariablesPHP StringPHP Operators
Overview PHP ArraysPHP If...ElsePHP SwitchPHP WhileFor LoopForeachFunctionForm,Get,Post
Overview ObjectDatabase ConnectionDatabase QuerySessions & Cookies
A brief history• Created by Rasmus Lerdorf in 1995
• PHP: Hypertext Preprocessor• PHP interpreters available on 32-bit and 64-bit operating systems
• Similar Syntax as C or Perl• PHP stands for PHP: Hypertext Preprocessor• PHP files have a file extension of ".php", ".php3",
or ".phtml"• PHP is a server-side scripting language, like ASP• PHP scripts are executed on the server• PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
• PHP is an open source software
What is PHP
• PHP runs on different platforms (Windows, Linux, Unix, etc.)
• PHP is compatible with almost all servers used today (Apache, IIS, etc.)
• PHP is FREE to download from the official PHP resource: www.php.net
• PHP is easy to learn and runs efficiently on the server side
Why PHP
The Stack•Apache (lighttpd, IIS)•MySQL (PostgresSQL, SQL Server, Oracle)
•PHP•PHPMyAdmin for adminisering your SQL Database
•Linux (BSD, Mac OS, Windows, Solaris)
The Stack
Syntax
<?phpecho "Hello World";?>
PHP Variables
<?php
$txt="Hello World!";
$x=16;
?>
PHP Variable Types Local Global Static Parameter
PHP Global Variable$a = 5; $b = 10;
function myTest()
{
global $a, $b;
$b = $a + $b;
}
myTest();
echo $b;
PHP Local Variable
$a = 5; // global scope
function myTest()
{
echo $a; // local scope
}
myTest();
PHP Static & Parameters
static $rememberMe;
function myTest($para1,$para2)
{
}
Operatorsx + y Addition Sum of x and y 2 + 2 4
x - y Subtraction Difference of x and y 5 - 2 3
x * y Multiplication Product of x and y 5 * 2 10
x / y Division Quotient of x and y 15 / 5 3
x % y Modulus Remainder of x divided by y 5 % 2
- x Negation Opposite of x - 2
a . b Concatenation Concatenate two strings "Hi" . "Ha" HiHa
x && y And True if both x and y are true
x || y Or True if either or both x and y are true
PHP Array$cars=array("Saab","BMW","Toyota");
$ages = array("ass1"=>32, "associative2"=>30);
$cars[0]="Saab";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1];
If else Statementif (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
}
Switch Statement$x=1;
switch ($x)
{
case 1:
echo "Test Number 1";
break;
case 2:
echo "Test Number 2";
break;
default:
echo "No test number between 1 and 2"; }
While Statement$i=1;
while($i<=5)
{
echo "This number is " . $i . "<br />";
$i++;
}
For Loop
for ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />";
}
Foreach Loop$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
Function<?php
function add($x,$y){$total=$x+$y;return $total;}echo "1 + 16 = " . add(1,16);?>
HTML Form<form action="welcome.php"
method="post">Name: <input type="text" name="fname" />Age: <input type="text" name="age" /><input type="submit" />
</form>
GET & POST Welcome <?php echo $_POST["fname"]; ?>
You are <?php echo $_POST["age"]; ?> years old.
OR
http://example.com/test.php?fname=Peter&age=37
Welcome <?php echo $_GET["fname"]; ?>.<br />You are <?php echo $_GET["age"]; ?> years old!
Mysql Connection$con =
mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("my_db", $con);mysql_close($con);
Mysql Query
$sql="INSERT INTO Persons (FirstName, Age)VALUES('$_POST[fname], '$_POST[age]')";
mysql_query($sql);
Select * FROM Persons
Update/Delete
Session & Cookiesession_start();
if(isset($_SESSION['views'])) unset($_SESSION['views]);
session_destroy();
setcookie(name,value,expire,domain,path,domain,secure,http only);
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", 1);
Thank you
Contact me @
http://www.linkedin.com/pub/mandakini-kumari/18/93/935
https://www.facebook.com/mandakini.kumari