Skip to content

Instantly share code, notes, and snippets.

Avatar

Richard Perez richardpq

  • Canada
View GitHub Profile
View gist:18e130dee507e883bcef
Verifying that +richardpq is my blockchain ID. https://onename.com/richardpq
@richardpq
richardpq / validate.php
Created Sep 17, 2015
Usually when you want to validate if the parameters you are receiving from a client has valid keys, you use a foreach to compare each parameter key with an array of valid keys, but this way is shorter and faster.
View validate.php
<?php
$invalidParameters = ['Key_1' => 1, 'Key_2' => 2, 'No_valid' => 3];
$validParameters = ['Key_1' => 1, 'Key_2' => 2];
function validParameters($parameters) {
$validKeys = ['Key_1', 'Key_2', 'Key_3', 'Key_4'];
$keys = array_keys($parameters);
return array_diff($keys, $validKeys) ? false : true;
View factorial.php
<?php
public function factorial($number)
{
if ($number == 1) {
return 1;
}
$total = $number * $this->factorial($number - 1);
return $total;
@richardpq
richardpq / magicTrick.php
Last active Aug 29, 2015
This is a very basic algorithm for a "magic trick" where you choose the same letter 3 times (in your mind) from a set of different letters distributed in 3 columns and the system will guess which one you choose.
View magicTrick.php
<?php
$letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U'];
shuffle($letters);
$count = 1;
$message = "";
do {
View SplClassLoader.php
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
You can’t perform that action at this time.