10 PHP
10 PHP
$int_var = 12345;
$many = 2.2888800;
$var= TRUE;
$my_var = NULL;
$string_1 = "This is a string in double quotes";
$string_2 = 'This is a somewhat longer, singly quoted string';
define("MINSIZE", 50); //constant type
10
11
12
13
<?php <?php
$d = date("D"); $d = date("D");
</body> else
</html> echo "Have a nice day!";
?>
</body>
</html>
14
15
loops <?php
$i = 0;
$num = 50;
</body>
16
</html>
Dr. Alekha Kumar Mishra
loops
<html>
<html> <body>
<body>
<?php
<?php $array = array( 1, 2, 3, 4, 5);
$i = 0;
$num = 0; foreach( $array as $value ) {
echo "Value is $value <br />";
do { }
$i++; ?>
}
</body>
while( $i < 10 ); </html>
echo ("Loop stopped at i = $i" );
?>
</body>
</html>
break keyword is used to terminate the
execution of a loop prematurely.
18
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
</body>
</html> 19
<?php
/* First method to associate create array. */
$salaries = array("steven" => 2000, "john" => 1000, "liza" => 500);
</body>
</html>
20
21
22
print($literally);
print "<br />";
print($literally);
?>
23
switch( $num ) {
case 1: $image_file = "/php/images/logo.png";
break;
</body>
</html> 25
26
exit();
}
?>
<html>
<body>
</body>
</html> 27
</select>
<input type = "submit" />
</form>
</body>
</html>
28
29
$orignum = 10;
addFive( $orignum );
echo "Original Value is $orignum<br />";
addSix( $orignum );
echo "Original Value is $orignum<br />"; 30
?>
Dr. Alekha Kumar Mishra
User-defined function
● A function can return a value <html>
<head>
using the return statement <title>Dynamic Function Calls</title>
</head>
– return $sum; <body>
<?php
● A function can return more than function sayHello() {
one value from a function using echo "Hello<br />";
return array(1,2,3,4). }
function sayHi() {
● We can also set a parameter to echo "Hi<br />";
}
have a default value if the
function's caller doesn't pass it $function_holder = "sayHello";
$function_holder();
using assignment operator $function_holder = "sayHi";
$function_holder();
– function myfunc($param = NULL)
{ ... } ?>
</body>
</html>
31
32
33
</body>
</html>
34
35
36
37
38
GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz
39
40
41
<head>
<title>Setting Cookies with PHP</title>
</head>
<body>
<?php echo "Set Cookies"?>
</body>
</html>
43
45
46
47
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
48
</html>
Dr. Alekha Kumar Mishra
Session Destroy
● Use session_destroy() function
– <?php session_destroy(); ?>
● to destroy a single session variable, use unset()
function
– <?php unset($_SESSION['counter']); ?>
● Turn Auto session on
– set session.auto_start variable to 1 in php.ini file
49
● ini_set('session.use_cookies', 0);
● ini_set('session.use_only_cookies', 0);
● ini_set('session.use_trans_sid', 1);
50
51
52
</body>
</html>
54
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysqli_close($conn);
?>
</body>
</html>
55
MYSQLI_USE_RESULT
MySQL the query string (To retrieve large amount of
connection data)
variable MYSQLI_STORE_RESULT
(default)
56
57
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
//Close connection
$conn->close();
58
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
echo $row["columnname"]. "<br>";
} else {
echo "0 results";
59
60
61
62