3 - PHP-MTD
3 - PHP-MTD
3 - PHP-MTD
PHP
PHP Stands for:
Past: Personal Home Page
Now: PHP Hypertext Preprocessor
Variable declaration
$namevariable = value;
Data types:
– Standard (boolean, int, float/double, string)
– Array
– NULL
– Resource (pre-defined variables)
Example: standard data types
$flag = true; (boolean)
$i = 2; (integer)
$j = 2.3; (float / double)
$name1 = "double"; (string double quote)
$name2 = 'single'; (string single quote)
Example: array data type
Initialization
//numeric index
$arr1 = array();
$arr2 = array(1303, "IT");
//associative index (map)
$arr3 = array("code"=>1303,
"name"=>"IT");
Call
echo $arr2[1]; //output: IT
echo $arr3["kode"]; //output: 1303
Example: array data type
Assignment
$arr2[1] = "S2 IF";
$arr3["code"] = 2301;
NULL data type
NULL is different from 0
$namavar = NULL;
//to check a variable isn’t NULL, use isset()
if (isset($namavar)) { //false
…
}
$namavar = 5;
if (isset($namavar)) { //true
…
}
Resource data type
Special variable for PHP information and specific operation
Example:
– $_SERVER["HTTP_HOST"];
– $_SESSION["user"];
– $_GET["id"];
POST method
– After form is submitted, data is stored as resource, not seen in URL
– How to get the submitted data: $_POST["nama_name"]
– nama_name is the value of your “name” attribute in HTML tag
When to use GET or POST
If data is confidential or for manipulation, use POST (example:
login, registration, data input, data update)
Other than above (data will be used generally), use GET (example:
searching, page/hyperlink, pagination)
Example: GET
find.php
<?php
echo "Result = ".$_GET["key"];
?>
formfind.php
<form method="get" action="find.php">
Keyword : <input type="text" name="key" /><br />
<input type="submit" value="Search" />
</form>
Example: POST
process.php
<?php
echo "User = ".$_POST["user"].",<br /> Pass = ".$_POST["pass"];
?>
formlogin.php
<form method="post" action="process.php">
Username: <input type="text" name="user" /><br />
Password: <input type="password" name="pass" /><br />
<input type="submit" value="Login" />
</form>
Session & Cookie
Resource variable to store data globally
<?php
session_start();
echo "Session ID Anda : ".session_id();
//... kode selanjutnya
?>
Session
Storing & accessing session
<?php
session_start();
if (!isset($_SESSION["counter"])) {
$_SESSION["nama"] = "prakwebprosession";
$_SESSION["counter"] = 0;
}
echo "<br /> Nama : ".$_SESSION["nama"];
$_SESSION["counter"]++;
echo "<br /> Anda telah melihat halaman ini sebanyak : ".
$_SESSION["counter"]." kali";
?>
Session
Deleting session
<?php
session_start();
unset($_SESSION["nama"]); //delete specific session data
Expire: Cookie expire time. For example, if you fill it with time()
+3600 then it will expire after 1 hour. If expire time is not set then
the cookie will be expired when the browser is closed (like session)
Cookie
Storing cookie
<?php
setcookie("nama","prakwebprocookie");
setcookie("nama2","cookie exp",time()+30);
?>
Cookie
Accessing cookie
<?php
echo "<br /> Nama kue : ".$_COOKIE["nama"];
echo "<br /> Nama kue : ".$_COOKIE["nama2"];
?>
Cookie
Deleting cookie
<?php
setcookie("nama","");
setcookie("nama2","",time()-30);
?>
Any question?
References
https://www.w3schools.com/php/default.asp
THANK YOU