Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Basics of PHP Slides

Uploaded by

fouzianawaz1997
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Basics of PHP Slides

Uploaded by

fouzianawaz1997
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

PHP

Server Scripting Language


PHP Syntax
<!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>

</body>
</html>
Variable Declaration
<?php <?php
$txt = "Hello world!"; $x = 10.365;
$x = 5; var_dump($x);
$y = 10.5; ?>
?>

Rules for PHP variables:


◦ A variable starts with the $ sign, followed by the name of the
variable
◦ A variable name must start with a letter or the underscore character
◦ A variable name cannot start with a number
◦ A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
◦ Variable names are case-sensitive ($age and $AGE are two different
variables)
how to output text and a variable
 <?php
$txt = “php scripting";
echo "I am learning $txt!";
?>
 <?php
$txt = “php scripting";
echo " I am learning " . $txt . "!";
?>
 <?php
$x = 5;
$y = 4;
echo $x + $y;
?>
PHP Globals
 <?php
$x = 5;
$y = 10;

function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] +
$GLOBALS['y'];
}

myTest();
echo $y; // outputs 15
?>
static Keyword
 <?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}

myTest();
myTest();
myTest();
?>
 $cars = array("Volvo","BMW","Toyota");
 echo strlen("Hello world!"); // outputs 12
 echo str_word_count("Hello world!"); //

outputs 2
 <?php

define("GREETING", "Welcome !");


echo GREETING;
?>
if-elseif-else
 <?php
$t = date("H");

if ($t < "10") {


echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Switch statement
 <?php
$favcolor = "red";

switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
looping
While loop For loop
<?php <?php
$x = 1; for ($x = 0; $x <=
10; $x++) {
while($x <= 5) { echo "The
echo "The number
number is: $x
is: $x <br>";
$x++;
<br>";
} }
?> ?>
foreach loop
 <?php  Output
$colors = array("red", red
"green", "blue",
green
"yellow");
blue
foreach ($colors as yellow
$value) {
echo "$value
<br>";
}
?>
Functions
 <?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year
<br>";
}

familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>
Array and looping
 <?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {


echo $cars[$x];
echo "<br>";
}
?>
PHP – Form
<html>
<body>

<form action="welcome.php" method="post">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>
……………………………welcome.php…………………….………
<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>


Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>
GET vs. POST
 Both GET and POST create an array
◦ e.g. array( key => value, key2 => value2, key3 => value3,.)
◦ This array holds key/value pairs, where keys are the names of
the form controls and values are the input data from the user.
 Both GET and POST are treated as $_GET and $_POST.
 These are superglobals, which means that they are
always accessible, regardless of scope
◦ and you can access them from any function, class or file
without having to do anything special.
 $_GET is an array of variables passed to the current
script via the URL parameters.
 $_POST is an array of variables passed to the current
script via the HTTP POST method.
When to use GET?
 Information sent from a form with the GET method
is visible to everyone (all variable names and
values are displayed in the URL). GET also has
limits on the amount of information to send. The
limitation is about 2000 characters. However,
because the variables are displayed in the URL, it
is possible to bookmark the page. This can be
useful in some cases.
 GET may be used for sending non-sensitive data.
 Note: GET should NEVER be used for sending
passwords or other sensitive information!
When to use POST?
 Information sent from a form with the POST
method is invisible to others (all names/values
are embedded within the body of the HTTP
request) and has no limits on the amount of
information to send.
 Moreover POST supports advanced functionality
such as support for multi-part binary input while
uploading files to server.
 However, because the variables are not displayed
in the URL, it is not possible to bookmark the page.
 Developers prefer POST for sending form
data.
Php: mysql_connect(,,)
 $host="localhost";
$user="root";
$password="";
$con=mysql_connect($host,$user,
$password);
if(!$con) {
echo '<h1>Connected to MySQL</h1>';
} else {
echo '<h1>MySQL Server is not
connected</h1>';
}
mysql_select_db, mysql_query
 $host="localhost";
$user="root";
$password="";
$con=mysql_connect($host,$user,$password);
if(!$con) {
echo '<h1>Connected to MySQL</h1>';
//if connected then Select Database.
$db=mysql_select_db("YOUR_DATABASE_NAME",$con);
$query=mysql_query("YOUR_MYSQL_QUERY",$db);
}
else {
echo '<h1>MySQL Server is not connected</h1>';
}
Retriev
al
using
Select …
with
mysql_fetch_arra
y()
&
MYSQL_ASSOC
Retrieva
l using
Select

with

mysql_fetch_assoc(
)
Retrieva
l using
Select

with
mysql_fetch_array(
)
&
MYSQL_NUM
Creating
new
Databas
e
Insert
into
Databas
e
Update
into
Databas
e
Delete from Database
action = "<?php $_PHP_SELF ?>“ -- for submitting to same page

You might also like