Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Tutorial Course

Download as pdf or txt
Download as pdf or txt
You are on page 1of 72

<?

php
echo "Hello World!";
?>
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>Embed PHP in HTML</title>
</head>
<body>
<h1>Embed PHP in HTML</h1>
<p>
<?php
echo "This is a paragraph.";
?>
</p>
<?php
echo "<p>This is another a paragraph.</p>";
?>
</body>

</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>variables</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Variables:</h1>
<p>
<?php
$name = "John";
$age = 33;
echo "My name is " . $name . ". I am " . $age . " years
old.";
?>
</p>
<p>
<?php
define("COUNTRY", "ENGLAND");
echo "Flight destination: " . COUNTRY;
?>
</p>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>data types: strings</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Strings:</h1>
<div>
<?php
$name = 'Peter';
echo 'Name: ' . $name . '<br />';
$string = "Peter's nice";
echo $string . "<br />";
$string2 = "My friend $name is nice.";
echo $string2;
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>data types: integers and floats</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Integers and Floats</h1>
<div>
<?php
$x = 7;
var_dump($x);
$y = 3;
$z = $x+$y;
echo "<br />";
echo $z;
$z = 0x1A;
echo "<br />";
var_dump($z);
$w = 0123;
echo "<br />";
var_dump($w);

$floatingNumber = 3.7;
echo "<br />";
var_dump($floatingNumber);
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>data types: booleans</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Booleans:</h1>
<div>
<?php
$booleanVariable1 = (5<6);
$booleanVariable2 = (3>5);
$booleanVariable3 = $booleanVariable1 ||
$booleanVariable2;
var_dump($booleanVariable1);
echo "<br />";
var_dump($booleanVariable2);
echo "<br />";
var_dump($booleanVariable3);
echo "<br />";
var_dump(!$booleanVariable1);
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>data types: arrays</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Arrays:</h1>
<div>
<?php
//Index Arrays
$carmakes = array("Audi", "BMW", "Mercedes");
echo "<p>Car makes:</p>";
print_r($carmakes);
echo "<p>Car makes: Element one</p>";
echo $carmakes[0];

//Associative Arrays
$shoppingBasket1 = array("a"=>"bread", "b"=>"milk",
"c"=>"eggs");
$shoppingBasket2 = array("b"=>"milk", "a"=>"bread",
"c"=>"eggs");
$shoppingBasket3 = array("d"=>"yogurt", "e"=>"orange",
"f"=>"apple");
$shoppingBasket = $shoppingBasket1 + $shoppingBasket3;
echo "<p>Shopping Basket:</p>";
print_r($shoppingBasket1);
echo "<br />";
var_dump($shoppingBasket1);
echo "<p>shoppingBasket1 == shoppingBasket2</p>";
var_dump($shoppingBasket1 == $shoppingBasket2);
echo "<p>shoppingBasket1 === shoppingBasket2</p>";
var_dump($shoppingBasket1 === $shoppingBasket2);
echo "<p>Basket 3</p>";
print_r($shoppingBasket3);
echo "<p>shoppingBasket1<> shoppingBasket3</p>";
var_dump($shoppingBasket1 != $shoppingBasket3);
echo "<p>shoppingBasket1 + shoppingBasket3</p>";
print_r($shoppingBasket1 + $shoppingBasket3);
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>data types: objects</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Objects:</h1>
<div>
<?php
class car{
//properties
public $make = "Ford";
private $status = "off";

//methods
function turn_on(){
$this->status = "on";
}
function getStatus(){
return $this->status;
}
}

$myCar = new car;


var_dump($myCar);
echo "<br />";
echo $myCar->make;
echo "<br />";
$myCar->turn_on();
var_dump($myCar);
echo "<br />";
echo $myCar->getStatus();
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>data types: NULL</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>NULL:</h1>
<div>
<?php
$novaluevariable;
var_dump($novaluevariable);
$novaluevariable = 5;
echo "<br />";
var_dump($novaluevariable);
$novaluevariable = null;
echo "<br />";
var_dump($novaluevariable);
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>data types: Resources</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Resources:</h1>
<div>
<?php
$myFile = fopen("sometext.txt","r");
var_dump($myFile);
echo "<br />";
echo fread($myFile,filesize("sometext.txt"));
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>data types: String Functions</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>String Functions:</h1>
<div>
<?php
$message = "Welcome to my website! Have a good time
exploring my website!";
echo "Length of my message: " . strlen($message);
echo "<br />";
echo "My message contains " . str_word_count($message) . "
words.";
echo "<br />";
echo str_replace("website", "world", $message,
$number_of_words_replaced);
echo "<br />";
echo "Number of words replaced: " .
$number_of_words_replaced . " words.";
echo "<br />";
echo "Can you read from the right? <br />" .
strrev($message);
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>If else and switch</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>If else and switch:</h1>
<di v>
<?php
$believeinyourdream = false;
if($believeinyourdream){
echo "<p>You will be successful.</p>";
}else{
echo "<p>Believe in your dream to be successful in
life.</p>";
}

$x = ($believeinyourdream)?"A":"B";
echo $x;

$temperature = 25;
if($temperature<15){
echo "<p>It is cold!</p>";
}elseif($temperature>30){
echo "<p>It is hot!</p>";
}else{
echo "<p>The temperature is medium.</p>";
}

$strength = "belief";
switch($strength){
case "belief":
echo "<p>You have core values that are unchanging.
They will define your purpose in life.</p>";
break;
case "communication":
echo "<p>You easily put your thoughts into words.
You are a good presenter.</p>";
break;
case "competition":
echo "<p>You measure your progress against the
progress of others. You strive to win first place</p>";
break;
default:
echo "<p>Choose a strength, you have many for
sure!</p>";
break;
}
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>For Loop</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>For Loop:</h1>
<div>
<?php
for($i = 1; $i <= 10; $i++){
echo $i . "<br />";
}
$carmakes = array("BMW", "Audi", "Mercedes");
foreach($carmakes as $value){
echo $value . "<br />";
}

$shoppingBasket = array("a"=>"yogurt", "b"=>"bread",


"c"=>"eggs");
foreach($shoppingBasket as $key=>$value){
echo $key . " : " . $value . "<br />";
}
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>While Loop</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>While Loop:</h1>
<div>
<?php
$i = 1;
while($i <= 10){
echo $i . "<br />";
$i++;
}

$carmakes = array("BMW", "Audi", "Mercedes");


$j = 0;
while($j<3){
echo $carmakes[$j] . "<br />";
$j++;
}
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Functions</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Functions:</h1>
<div>
<?php
function welcomeMe(){
echo "<p>Welcome to my website!</p>";
}
function welcomeUser($name, $credit){
echo "<p>Hi $name. Welcome to my website! Your credit
is $credit dollars.</p>";
}

function sum($x, $y){


return $x + $y;
}

welcomeMe();
welcomeUser("John", 100);
$sum = sum(7,5);
echo "<p>The sum of 7 and 5 is $sum.</p>";

?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>GET AND POST</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Get and Post:</h1>
<div>
<?php
echo "<h3>GET:</h3>";
print_r($_GET);
if($_GET["submit"]){
if($_GET["username"]) {
echo "<p>Hi ". $_GET["username"] . " . Welcome to
my page!</p>";
}
}
echo "<h3>POST:</h3>";
print_r($_POST);
if($_POST["submit"]){
if($_POST["country"]){
echo "<p>Your Country is: " . $_POST["country"] .
".</p>";
}
}
?>
<form method="get"
action="16.GET&POST.php">
<label
for="username">Username:</label>
<input type="text" name="username"
id="username">
<input type="submit" name="submit"
value="Submit">
</form>
<form method="post"
action="16.GET&POST.php">
<label
for="country">Country:</label>
<input type="text" name="country"
id="country">
<input type="submit" name="submit"
value="Submit">
</form>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Array functions</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Array Functions:</h1>
<div>
<?php
$shoppingBasket1 = array("bread", "milk", "eggs",
"bread");
$shoppingBasket2 = array("apple", "banana", "orange");
$shoppingBasket = array_merge($shoppingBasket1,
$shoppingBasket2);
echo "<p>Shopping Basket:</p>";
print_r($shoppingBasket);
echo "<p>Number of items in Shopping basket: </p>" .
count($shoppingBasket);

$count = array_count_values($shoppingBasket);
echo "<p>Basket Count</p>";
print_r($count);
echo "<p>Number of bread iteams in Basket: </p>" .
$count["bread"];

if(in_array("bread", $shoppingBasket)){
echo "<p>There is bread in the basket.</p>";
}else{
echo "<p>There is no bread in the basket.</p>";
}

array_push($shoppingBasket,"yogurt");
echo "<p>Shopping Basket after adding yogurt: </p>";
print_r($shoppingBasket);
if($_GET["submit"]){
if($_GET["item"]){
array_push($shoppingBasket,$_GET["item"]);
}
}
echo "<p>Shopping Basket:</p>";
print_r($shoppingBasket);

array_splice($shoppingBasket, 0, 4,array("mango",
"kiwi"));
echo "<p>Shopping Basket:</p>";
print_r($shoppingBasket);

sort($shoppingBasket);
echo "<p>Shopping Basket sorted in ascending order:</p>";
print_r($shoppingBasket);

$carMakes = array("BMW"=>"X5", "Audi"=>"A6",


"Mercedes"=>"CLS");
echo "<p>Car makes:</p>";
print_r($carMakes);
asort($carMakes);
echo "<p>Car makes sorted in ascending order by
value:</p>";
print_r($carMakes);
ksort($carMakes);
echo "<p>Car makes sorted in ascending order by
keys:</p>";
print_r($carMakes);
?>
<form method="get">
<label for="item">Add item to
shopping Basket:</label>
<input type="text" name="item"
id="item">
<input type="submit" name="submit"
value="Submit">
</form>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Send Email</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Send Email:</h1>
<div>
<?php
$to = "sam@developwithsam.890m.com";
$subject = "We are learning PHP!";
$message = "<html><body><h1 style='color:green'>We are
learning PHP!</h1><h3 style='color:orange'>What are you
waiting for?</h3><p>Join us and spread the
word!</p></body></html>";
$headers = "Content-type: text/html;";
echo mail($to, $subject, $message, $headers);
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Filters</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color: #5ee875;
}
</style>

</head>
<body>
<div class="container-fluid">
<h1>Filters:</h1>
<h3>Clean user inputs</h3>
<div>
<?php
//username example
$myUsername = '<script>window.alert("Hi")</script>';
$myUsername = filter_var($myUsername,
FILTER_SANITIZE_STRING);
echo $myUsername;
//email example
$myEmail = 'sam@ completeweb developm\\\\ entcour
se.co.uk';
$myEmail = filter_var($myEmail, FILTER_SANITIZE_EMAIL);
echo "<br />" . $myEmail;
//URL example
$myURL = "http://££www. google.com";
$myURL = filter_var($myURL, FILTER_SANITIZE_URL);
echo "<br />" . $myURL;
?>
</div>
<h3>Validate user inputs</h3>
<div>
<?php

//Email validation
$myEmail = 'sam completeweb developm\\\\ entcour
se.co.uk';
$myEmail = filter_var($myEmail, FILTER_SANITIZE_EMAIL);
echo "<p>Cleaned email: $myEmail</p>";
echo "<p>Email validation: " . filter_var($myEmail,
FILTER_VALIDATE_EMAIL) . "</p>";
if(filter_var($myEmail, FILTER_VALIDATE_EMAIL)){
echo "<p>Valid Email</p>";
}else{
echo "<p>Invalid Email</p>";
}
//URL validation
$myURL = "http://££www. google.com";
$myURL = filter_var($myURL, FILTER_SANITIZE_URL);
echo "<p>Cleaned URL: $myURL</p>";
echo "<p>URL validation: ". filter_var($myURL,
FILTER_VALIDATE_URL) ."</p>"
?>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!--20.contactform.php-->
<?php ob_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Contact Form</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
.contactForm{
border:1px solid #7c73f6;
margin-top: 50px;
border-radius: 15px;
}
</style>

</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
contactForm">
<h1>Contact us:</h1>
<?php

//Get user input


$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

//error messages
$missingName = '<p><strong>Please enter your
name!</strong></p>';
$missingEmail = '<p><strong>Please enter your email
address!</strong></p>';
$invalidEmail = '<p><strong>Please enter a valid email
address!</strong></p>';
$missingMessage = '<p><strong>Please enter a
message!</strong></p>';

//if the user has submitted the form


if($_POST["submit"]){
//check for errors
if(!$name){
$errors .= $missingName;
}else{
$name = filter_var($name,FILTER_SANITIZE_STRING);
}
if(!$email){
$errors .= $missingEmail;
}else{
$email = filter_var($email,
FILTER_SANITIZE_EMAIL);
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
$errors .=$invalidEmail;
}
}
if(!$message){
$errors .= $missingMessage;
}else{
$message = filter_var($message,
FILTER_SANITIZE_STRING);
}

//if there are any errors


if($errors){
//print error message
$resultMessage = '<div class="alert alert-
danger">' . $errors .'</div>';
}else{
$to = "sam@hellodevelopers.890m.com";
$subject = "Contact";
$message = "
<p>Name: $name.</p>
<p>Email: $email.</p>
<p>Message:</p>
<p><strong>$message</strong></p>";
$headers = "Content-type: text/html";
if(mail($to, $subject, $message, $headers)){
// $resultMessage = '<div class="alert alert-
success">Thanks for your message. We will get back to you
as soon as possible!</div>';
header("Location:
20.thanksforyourmessage.php");
}else{
$resultMessage = '<div class="alert alert-
warning">Unable to send Email. Please try again
later!</div>';
}
}
echo $resultMessage;
}
?>
<form action="20.contactform.php"
method="post">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" id="name"
placeholder="Name" class="form-control" value="<?php echo
$_POST["name"];?>">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" name="email"
id="email" placeholder="Email" class="form-control"
value="<?php echo $_POST["email"];?>">
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea name="message" id="message"
class="form-control" rows="5"><?php echo
$_POST["message"];?></textarea>
</div>
<input type="submit" name="submit"
class="btn btn-success btn-lg" value="Send Message">

</form>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<?php ob_flush(); ?>

<!--20.thanksforyourmessage.php-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Thanks for your message</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
.contactForm{
border:1px solid #7c73f6;
margin-top: 50px;
border-radius: 15px;
}
</style>

</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
contactForm">
<h1>Thanks</h1>
<div class="alert alert-success">Thanks for
your message. We will get back to you as soon as
possible!</div>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Date and Time</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.contactForm{
border:1px solid #7c73f6;
margin-top: 50px;
border-radius: 15px;
}
</style>

</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
contactForm">
<h1>Date and Time</h1>
<div>
<h3>Date using the date() function:</h3>
<?php
//Formatting the Dates and Times with PHP
// Day of the month:
//d (01/31)->two digits with leading zeros
//j (1/31)->without leading zeros
// Day of the week:
//D (Mon/Sun)->text as an abbreviation
//l (monday)->full lowercase
//L (MONDAY)->full uppercase
// Month:
//m (01/12)->two digits with leading zeros
//M (Jan)->text as an abbreviation
//F (January)-> Full month
// Year:
//y (09/15)->two digits
//Y (2009-2015)->Four Digits
// Separators:
//hyphens: (-)
//dots: (.)
//slashes: (/)
//spaces: ( )
$today = date("j-m, y");
echo "<p>Today is: $today.</p>";

?>

<h3>Time using the date() function:</h3>


<?php
//format the time string:
//hour:
//h (01-12)->12-hour format with leading zeros
//H (00-24)->24-hour format with leading zeros
//minutes:
//i (01-59)->minutes with leading zeros
//seconds:
//s (01-59) ->seconds with leading zeros
//Ante meridiem and Post meridiem
//a->lowercase
//A->uppercase

$today = date("H:i:s A");


echo "<p>Time is: $today.</p>";

?>

<h3>Current timestamp using time()


function:</h3>
<?php
$timestamp = time();
echo "<p>Timestamp is : $timestamp</p>";
?>

<h3>Convert timestamp to time:</h3>


<?php
$time = date("F d, Y h:i:s A", $timestamp);
echo "<p>Time is $time.</p>";
?>

<h3>Convert time to timestamp using


mktime() function:</h3>
<?php
//mktime(hour,minute,second,month,day,year)
$timestamp = mktime(15, 20, 12, 5, 10, 2017);
echo "<p>Timestamp is : $timestamp</p>";
$time = date("F d, Y h:i:s A", $timestamp);
echo "<p>Time will be $time.</p>";
?>

<h3>Find out what day of the week you were


born:</h3>
<?php
$timestamp = mktime (0,0,0,15,04,1977);
$time = date("l",$timestamp);
echo "<p>You were born on a $time.</p>";
?>

<h3>Date in 1000 days from now:</h3>


<?php
$timestamp = mktime(0, 0, 0, date("m"), date("d")+1000,
date("Y"));
$time = date("D d M, Y", $timestamp);
echo "<p>Date in 1000 days from now: $time.</p>";

?>

</div>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!--23.includefiles.php-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Include Files</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>
<body>
<?php
include "header.php";
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Include Files</h1>
</div>
</div>
</div>
<?php
include "footer.php";
?>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<!--header.php-->
<nav role="navigation" class="navbar navbar-inverse
navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand">Store</a>
<button type="button" class="navbar-toggle"
data-target="#navbarCollapse" data-toggle="collapse">
<span class="sr-only">Toggle
navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse"
id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">All
Products</a></li>
<li><a href="#">Help</a></li>
<li><a href="#">Department <span
class="caret"></span></a></li>
</ul>
<form class="navbar-form navbar-left"
role="search">
<div class="input-group">
<span class="input-group-btn">
<button type="submit" class="btn btn-
info">Go</button>
</span>
<label for="search" class="sr-
only">Search</label>
<input type="text" id="search"
class="form-control" placeholder="Search">
<span class="glyphicon glyphicon-
search form-control-feedback"></span>
</div>
</form>

<form class="navbar-form navbar-right">


<input class="btn btn-success" type="button"
value="Login">
</form>
</div>
</div>
</nav>

<!--footer.php-->
<style>
html{
position: relative;
min-height:100%;
}
body{
margin-bottom: 100px;
}
.footer{
position: absolute;
bottom: 0;
width: 100%;
background-color: #f3f1f1;
height: 60px;
}

</style>

<footer class="footer">
<div class="container">
<p class="text-muted">
<?php
$today = date("Y");
echo "Copyright &copy; 2010-$today";
?>
</p>
</div>

</footer>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>File Handling</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>

</head>
<body>
<?php
include "header.php";
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>File Handling</h1>

<h3>fopen/fclose:</h3>
<?php
if(file_exists("sometextf.txt")){
$myFile = fopen("sometext.txt", "r");
fclose($myFile);
}else{
echo "<p>This file does not exist!</p>";
}

?>

<h3>fread:</h3>
<?php
$fileHandle = fopen("sometext.txt", "r") or die("Unable to
open file!");
$fileContent =
fread($fileHandle,filesize("sometext.txt"));
fclose($fileHandle);
var_dump($fileContent);
?>

<h3>file_get_contents:</h3>
<?php
$fileContent = file_get_contents("sometext.txt") or
die("Unable to read file");
var_dump($fileContent);

?>

<h3>file function:</h3>
<?php
$fileContent = file("sometext.txt") or die ("Unable to
read file");
var_dump($fileContent);
echo "<br />";
foreach($fileContent as $line){
echo $line . "<br />";
}
?>

<h3>fwrite:</h3>
<?php
$fileHandle = fopen("sometext.txt", "w") or die ("Unable
to open file!");
fwrite($fileHandle, "This is new content") or die("Unbale
to write to file!");
$fileContent = file("sometext.txt") or die("Unable to read
file!");
var_dump($fileContent);
?>

<h3>file_put_contents:</h3>
<?php
file_put_contents("sometext.txt", "\r\n This is some other
new content!", FILE_APPEND) or die ("Unable to write to
file!");
$fileContent = file("sometext.txt") or die("Unable to read
file!");
var_dump($fileContent);
?>

</div>
</div>
</div>
<?php
include "footer.php";
?>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!--25.fileupload.php-->
<?php ob_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>File Upload</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>
<body>
<?php
include "header.php";
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Upload file:</h1>
<?php
if($_POST["submit"]){
//file details
$name = $_FILES["file"]["name"];
$type = $_FILES["file"]["type"];
$size = $_FILES["file"]["size"];
$fileerror = $_FILES["file"]["error"];
$tmp_name = $_FILES["file"]["tmp_name"];
$permanentdestination = "uploads/" .
$_FILES["file"]["name"];

//error messages to display


$noFiletoUpload = "<p><strong>Please upload a
file!</strong></p>";
$fileAlreadyExists = "<p><strong>This file already
exists!</strong></p>";
$wrontFormat = "<p><strong>Sorry, you can only upload
pdf and text files!</strong></p>";
$fileTooLarge = "<p><strong>You can only upload files
smaller than 3Mo!</strong></p>";

//allowed formats to upload


$allowedFormats = array("pdf"=>"application/pdf",
"text"=>"text/plain");

//check for errors


if($fileerror == 4){
$errors .=$noFiletoUpload;
}else{
if(file_exists($permanentdestination)){
$errors .= $fileAlreadyExists;
}
if($size > 3*1024*1024){
$errors .= $fileTooLarge;
}
if(!in_array($type, $allowedFormats)){
$errors .= $wrontFormat;
}
}

if($errors){
$resultMessage = '<div class="alert alert-
danger">' . $errors .'</div>';
echo $resultMessage;
}else{
if(move_uploaded_file($tmp_name,
$permanentdestination)){
// $resultMessage = '<div class="alert alert-
success">File uploaded successfuly</div>';
// echo $resultMessage;
header("Location: 26.thanksforyourfile.php");
}else{
$resultMessage = '<div class="alert alert-
warning"Unable to upload file. Please try again
later.></div>';
echo $resultMessage;
}

print_r($_FILES);
if($_FILES["file"]["error"]>0){
echo "<p>There was an error: ".
$_FILES["file"]["error"] ."</p>";
}else{
echo "<p>File: ". $_FILES["file"]["name"]
."</p>";
echo "<p>File type: ". $_FILES["file"]["type"]
."</p>";
echo "<p>Temporary location: ".
$_FILES["file"]["tmp_name"] ."</p>";
echo "<p>File size: ". $_FILES["file"]["size"]
."</p>";
echo "<p>Permanent destination: ". "uploads/" .
$_FILES["file"]["name"] . "</p>";
}
}

?>
<form method="post" enctype="multipart/form-
data">
<div class="form-group">
<label for="file">Choose file:</label>
<input type="file" name="file"
id="file" placeholder="File">
</div>
<input type="submit" name="submit"
class="btn btn-success btn-lg" value="Upload">
</form>
</div>
</div>
</div>
<?php
include "footer.php";
?>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<?php ob_flush(); ?>

<!--26.thanksforyourfile.php-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Thanks for your file</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
.contactForm{
border:1px solid #7c73f6;
margin-top: 50px;
border-radius: 15px;
}
</style>

</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
contactForm">
<h1>Thanks</h1>
<div class="alert alert-success">Thanks for
your file. We will get back to you as soon as
possible!</div>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Cookies</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>

</head>
<body>
<?php
include "header.php";
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Cookies:</h1>
<?php
setcookie("username", "buenosdias", time()+7*24*60*60);
setcookie("username", "", time()-1000);
print_r($_COOKIE);
if($_COOKIE["username"]){
echo "<p>Value of the username cookie: ".
$_COOKIE["username"] ."</p>";
}else{
echo "<p>Cookie not set.</p>";
}
?>
</div>
</div>
</div>
<?php
include "footer.php";
?>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Error Handling</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>

</head>
<body>
<?php
include "header.php";
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Error Handling:</h1>
<h3>Example 1:</h3>
<?php
function errorHandler1($errno, $errstr, $errfile,
$errline, $errcontext){
echo "<p><strong>Error</strong>: [$errno]
$errstr.</p>";
}
// Set error handler
set_error_handler("errorHandler1");
echo filesize("inexistingfile.txt");
?>
<h3>Example 2:</h3>
<?php
function calculateFileSize($file){
if(!file_exists($file)){
trigger_error($file . " does not exist and thus
size cannot be retrieved!", E_USER_WARNING);
return false;
}else{
return filesize($file);
}
}

function errorHandler2($errno, $errstr, $errfile,


$errline, $errcontext){
$log = "Error[$errno] on " . date("d/m/Y H:i:s") .
"\r\n";
$log .= "Details: $errstr. \r\n";
$log .= "Location: In $errfile on line $errline.
\r\n";
$log .= "Variables: " . print_r($errcontext, true) .
"\r\n";
error_log($log, 3, "logs/errorhandlingerrors.log");
error_log($log, 1, "sam@hellodevelopers.890m.com");
die("<p>An error occured, please try again!</p>");
}

set_error_handler("errorHandler2");
echo calculateFileSize("inexistingfile.txt");

?>
</div>
</div>
</div>
<?php
include "footer.php";
?>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>SESSIONS</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>

</head>
<body>
<?php
include "header.php";
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>SESSIONS:</h1>
<?php
// start a session
session_start();
// store data
$_SESSION["firstname"] = "Mark";
$_SESSION["lastname"] = "Zuckerberg";
$firstname = $_SESSION["firstname"];
$lastname = $_SESSION["lastname"];
echo "<p>Hi $firstname $lastname!</p>";
//remove data from session
if(isset($_SESSION["firstname"])){
unset($_SESSION["firstname"]);
}
echo $_SESSION["firstname"]?1:0;
//destroy session
session_destroy();
?>
</div>
</div>
</div>
<?php
include "footer.php";
?>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

You might also like