Basics of PHP Slides
Basics of PHP Slides
<?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; ?>
?>
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
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);
</body>
</html>
……………………………welcome.php…………………….………
<html>
<body>
</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