Skip to content

Instantly share code, notes, and snippets.

View daveh's full-sized avatar

Dave Hollingworth daveh

View GitHub Profile
@daveh
daveh / php_equality_operators.php
Last active July 9, 2019 08:27
PHP equality operators example (from https://youtu.be/SptQ2LhtxnE)
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>Example</h1>
<form>
@daveh
daveh / php_assignments_and_conditionals_example1.php
Last active October 1, 2019 08:13
Example 1 of assignments inside conditionals (https://youtu.be/UaxP0aMeT5w)
<?php
$array = ['red', 'green', 'blue'];
$quantity = count($array);
if ($quantity)
//if ($quantity = count($array))
{
echo "There are $quantity items";
@daveh
daveh / php_assignments_and_conditionals_example2.php
Created October 1, 2019 08:14
Example 2 of assignments inside conditionals (https://youtu.be/UaxP0aMeT5w)
<?php
$array = ['red', 'green', 'blue'];
$quantity = count($array);
// if ($quantity == 5)
//if ($quantity = 5)
if (5 == $quantity)
{
@daveh
daveh / string_concatenation.php
Last active November 13, 2019 11:18
Concatenating strings in PHP (https://youtu.be/sOmHCk-_UaM)
// Code to accompany this video: https://youtu.be/sOmHCk-_UaM
// 1. The concatenation operator
$message = "Hello " . "world"; // $message contains "Hello world"
// 2. The concatenating assignment operator
$title = "Home";
$title .= " | About"; // $title now contains "Home | About"
// 3. Splitting concatenations onto more than one line
@daveh
daveh / php_null_coalescing.php
Last active January 20, 2020 19:31
The PHP null coalescing operator https://youtu.be/faoGMg-n07Y
<?php
// Code to accompany this video: https://youtu.be/faoGMg-n07Y
// 1. No checking for the variable being set
$page = $_GET['page'];
// 2. Using isset to check that the variable is set
if (isset($_GET['page'])) {
@daveh
daveh / form.php
Created January 29, 2020 10:26
Client-side vs. server-side validation (https://youtu.be/O3ewos-_iME)
<!DOCTYPE html>
<html>
<head>
<title>Validation example</title>
</head>
<body>
<h1>Subscribe to our newsletter</h1>
<form method="post" action="process_form.php">
@daveh
daveh / index.php
Created March 1, 2020 17:46
Checking if a form has been submitted in PHP (https://youtu.be/tORiyyMds1M)
<?php
// Code to accompany this video: https://youtu.be/tORiyyMds1M
// if ($_POST) {
// if (isset($_POST['sendButton'])) {
if($_SERVER['REQUEST_METHOD'] == 'POST') {
exit("form submitted");
@daveh
daveh / after_exceptions.php
Created April 5, 2020 19:48
Refactoring nested conditionals in PHP (code to accompany https://youtu.be/xQRMHGy4xCM)
<?php
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
throw new Exception('an error occurred');
}
if ($_FILES['file']['size'] > 1000000) {
@daveh
daveh / after.php
Created May 8, 2020 10:19
Refactoring PHP: code layout and coding standards (code to accompany https://youtu.be/KD8l5rJxN1Y)
<?php
namespace App;
class Article
{
private $title;
public function __construct($title)
{
@daveh
daveh / index.php
Created June 2, 2020 13:31
Why you should use curly braces in conditionals and loops (code to accompany https://youtu.be/dsu0ooy2hnI)
<?php
/*
if ( ! file_exists("input.txt"))
exit;
*/
/*
if ( ! file_exists("input.txt"))
echo "File not found\n";