1) Cookie
Html:
<html>
<body>
<form action=" [Link]" method="post">
<label> Enter Name: </label>
<input type="text" name="username"><br><br>
<input type="submit" name="set" value="Set Cookie"><br><br>
<input type="submit" name="update" value="Update Cookie"><br><br>
<input type="submit" name="show" value="Show Cookie"><br><br>
<input type="submit" name="delete" value="Delete Cookie"><br><br>
</form>
</body>
</html>
Php
<?php
if(isset($_POST['set']))
$username=$_POST['username'];
setCookie("user", $username, time()+(86400*30), "/");
echo "Cookie has been set to: $username<br><br>";
}
if(isset($_POST['update']))
$username=$_POST['username'];
setCookie("user", $username, time()+(86400*30), "/");
echo "Cookie has been updated to: $username<br><br>";
if(isset($_POST['delete']))
setCookie("user", "", time()-3600 , "/");
echo "Cookie has been deleted.<br><br>";
if(isset($_POST['show']))
if(isset($_COOKIE['user']))
echo "Current cookie value: ". $_COOKIE['user']."<br><br>";
else
echo "Cookie is not set.";
?>
2) Session
Html:
<html>
<body>
<form action="[Link]" method="post">
<label> Enter Name:</label>
<input type="text" name="username"><br><br>
<input type="submit" name="set" value="Set Session"><br><br>
<input type="submit" name="get" value="Get Session"><br><br>
<input type="submit" name="destroy" value="Destroy Session"><br><br>
</form>
</body>
</html>
Php
<?php
session_start();
if(isset($_POST['set']))
$_SESSION['user']=$_POST['username'];
echo "Session is set to: ".$_SESSION['user']."<br><br>";
if(isset($_POST['get']))
{
if(isset($_SESSION['user']))
echo "Session value is: ".$_SESSION['user']."<br><br>";
else
echo "Session is not set";
if(isset($_POST['destroy']))
session_unset();
session_destroy();
echo "Session destroyed";
?>
3) Web page validation
Html
<html>
<body>
<form action="web_validation.php" method="post">
Name: <input type="text" name="t1">
Mobile: <input type="text" name="mob">
Username: <input type="text" name="user">
<input type="submit" >
</form>
</body>
</html>
Php
<?php
if(empty($_POST['t1']))
echo "Enter valid Text";
else
echo "Name: ".$_POST['t1'];
if(!is_numeric($_POST['mob']))
echo "Enter valid Number";
else
echo "Mobile: ".$_POST['mob'];
$n=$_POST['user'];
$p="/^[a-z0-9]*$/";
if(!preg_match($p,$n))
echo "Enter valid Username";
else
echo "Username: ".$_POST['user'];
?>
4) Design a web page using following form controls: a. Text box, b. Radio
button, c. Check box, d. Buttons
Html
<html>
<body>
<form action="[Link]" method="post">
Name: <input type="text" name="t1">
<label>Gender:</label>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
<input type="radio" name="gender" value="Other">Other
<label>Hobbies:</label>
<input type="checkbox" name="hobby" value="singing">singing
<input type="checkbox" name="hobby" value="dancing">dancing
<input type="checkbox" name="hobby" value="drawing">drawing
<input type="checkbox" name="hobby" value="cooking">cooking
<input type="submit">
</form></body></html>
Php
<?php
if(isset($_POST['t1']))
echo "Name:". $_POST['t1'];
if(isset($_POST['gender']))
echo "Gender:".$_POST['gender'];
if(isset($_POST['hobby']))
echo "Hobby:".$_POST['hobby'];
?>
5) Design a web page using following form controls: a. List box, b. Combo box,
c. Hidden field box
Html
<html>
<body>
<form action="[Link]" method="post">
<label>Your country:</label>
<select name="country">
<option>India</option>
<option>Us</option>
<option>Canada</option></select>
<label>You hobbies:</label>
<select name="hobby" multiple>
<option>Singing</option>
<option>drawing</option>
<option>dancing</option>
<option>cooking</option>
</select>
<input type="hidden" name="user" value="saee">
<input type="submit">
</form>
</body>
</html>
Php
<?php
if(isset($_POST["country"])) {
echo "Your country is: " . $_POST["country"] . "<br>";
if(isset($_POST["hobby"])) {
echo "Your hobby is: " . $_POST["hobby"] . "<br>";
if(isset($_POST["user"])) {
echo "Your User ID is: " . $_POST["user"] . "<br>";
?>
6) Introspection
<?php
Trait traitsample
public function sayHi()
echo "Hello from trait";
Interface interfacesample
public function greet();
}
Class classsample implements interfacesample
use traitsample;
public $name="saee";
public $age=19;
public function greet()
echo "Hello from interface";
public function sample()
echo "Hello from class";
$obj=new classsample();
echo "using get_class() method:". get_class($obj);
echo "<br>";
echo "<br>";
echo "using get_class_methods(): ";
print_r(get_class_methods("classsample"));
echo "<br>";
echo "<br>";
echo "using get_class_vars(): ";
print_r(get_class_vars("classsample"));
echo "<br>";
echo "<br>";
echo "using class_exists(): ";
if(class_exists("classsample"))
echo "Class Exists<br>";
else
echo "Class do not exists<br>";
echo "<br>";
echo "<br>";
echo "using interface_exists(): ";
if(interface_exists("interfacesample"))
echo "interface Exists<br>";
else
echo "interface do not exists<br>";
echo "<br>";
echo "<br>";
echo "using trait_exists(): ";
if(trait_exists("traitsample"))
echo "Trait Exists<br>";
else
echo "Trait do not exists<br>";
echo "<br>";
echo "<br>";
?>
7) Serialization
<?php
$a = serialize(array("Welcome", "to", "PHP"));
echo "Serialization:<br>";
echo $a . "<br>";
$a_un = unserialize($a);
echo "Unserialization:<br>";
print_r($a_un);
?>
8) PDF
<?php
require('[Link]');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',20);
$pdf->Cell(40,10,'Hii, Welcome Saee’);
$pdf->Output();
?>
9) Array
<?php
echo "<h3>Indexed Array:</h3>";
$fruits=array("apple","banana","mango");
foreach($fruits as $fruit)
echo $fruit."<br>";
echo "<h3>Associative Array:</h3>";
$student=array("name"=>"saee","age"=>19,"city"=>"pune");
foreach($student as $key=>$value)
echo $key .":".$value."<br>";
echo "<h3>Multidimensional Array:</h3>";
$students=array(
array("saee",19,"pune"),
array("john",20,"mumbai"),
array("peter",30,"nashik"));
print_r($students);
?>
10) String
<?php
$str="hello php";
$arr=array("blue","red","pink");
echo "strlen function: ";
echo strlen("Hello World");
echo "<br>";
echo "str_word_count function: ";
echo str_word_count("hello world");
echo "<br>";
echo "strrev function: ";
echo strrev("hello world");
echo "<br>";
echo "strpos function: ";
echo strpos($str,"php");
echo "<br>";
echo "strreplace function: ";
print_r (str_replace("red","yellow",$arr));
echo "<br>";
echo "strcmp function: ";
echo strcmp("hello world","hello world");
echo "<br>";
echo "strtolower function: ";
echo strtolower("HELLO");
echo "<br>";
echo "strtoupper function: ";
echo strtoupper("hello");
echo "<br>";
echo "ucwords function: ";
echo ucwords("welcome to php programming");
?>
11) For loop
<?php
for ($num = 1; $num<= 100; $num += 5)
echo $num . " <br> ";
?>
12) While loop
<?php
$num = 1;
while ($num<= 10) {
echo $num . " <br> ";
$num++;
?>
13) Do while loop
<?php
$num = 5;
do {
echo $num . " ";
$num++;
} while ($num<= 10);
?>
14) If statement
<?php
$a=10;
if($a==10)
echo "Number is equal to 10 <br>";
echo "Statment X";
15) If else
<?php
$x = -15;
if ($x > 0)
echo "The number is positive";
else
echo "The number is negative";
?>
16) Switch
<?php
$day = "Tuesday";
switch($day) {
case "Monday":
echo "Start of the week!";
break;
case "Tuesday":
echo "It's Tuesday!";
break;
case "Wednesday":
echo "Midweek day!";
break;
default:
echo "Another day!";
?>
17) Operators
<?php
// 1. Arithmetic Operators
$a = 10;
$b = 5;
echo "Addition: " . ($a + $b) . "<br>"; // 15
echo "Subtraction: " . ($a - $b) . "<br>"; // 5
echo "Multiplication: " . ($a * $b) . "<br>"; // 50
echo "Division: " . ($a / $b) . "<br>"; // 2
echo "Modulus: " . ($a % $b) . "<br>"; // 0
// 2. Assignment Operators
$c = $a; // = operator
$c += 5; // += operator
echo "Assignment result: $c<br>"; // 15
// 3. Comparison Operators
echo "Is a equal to b? " . ($a == $b) . "<br>"; // false (empty)
echo "Is a not equal to b? " . ($a != $b) . "<br>"; // true (1)
// 4. Logical Operators
$x = true;
$y = false;
echo "x AND y: " . ($x && $y) . "<br>"; // false (empty)
echo "x OR y: " . ($x || $y) . "<br>"; // true (1)
// 5. Increment/Decrement Operators
$num = 3;
echo "Post-increment: " . ($num++) . "<br>"; // 3
echo "After increment: $num<br>"; // 4
echo "Pre-decrement: " . (--$num) . "<br>"; // 3
// 6. String Operators
$str1 = "Hello ";
$str2 = "World!";
echo "Concatenation: " . ($str1 . $str2) . "<br>"; // Hello World!
// 7. Array Operators
$arr1 = array("a" => 1, "b" => 2);
$arr2 = array("b" => 2, "a" => 1);
echo "Are arrays equal? " . ($arr1 == $arr2) . "<br>"; // true (1)
$a = 6; // 0110 in binary
$b = 3; // 0011 in binary
echo "<h3>Bitwise Operators in PHP</h3>";
echo "a = $a, b = $b<br><br>";
echo "1. AND (&): a & b = " . ($a & $b) . "<br>"; // 0110 & 0011 = 0010 => 2
echo "2. OR (|): a | b = " . ($a | $b) . "<br>"; // 0110 | 0011 = 0111 => 7
echo "3. XOR (^): a ^ b = " . ($a ^ $b) . "<br>"; // 0110 ^ 0011 = 0101 => 5
?>