Skip to content

Instantly share code, notes, and snippets.

@AlexTiTanium
Created November 9, 2012 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexTiTanium/4047363 to your computer and use it in GitHub Desktop.
Save AlexTiTanium/4047363 to your computer and use it in GitHub Desktop.
PHP 5.5 new
function addRecord($record, $db) {
try {
$db->lock($record->table);
$db->prepare($record->sql);
$db->exec($record->params);
}
catch(Exception $e) {
$db->rollback($record);
if (!write_log($e->getMessage(), $e->getTraceAsString())) {
throw new Exception('Unable to write to error log.');
}
}
finally {
$db->unlock($record->table);
}
return true;
}
function make_blowfish_salt() {
$alphabet = array_merge(range('A','Z'), range('a','z'), range(0,9), array('.','/'));
shuffle($alphabet);
$salt = '';
if (($iv = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM)) === false) {
trigger_error("Unable to initialize vector from mcrypt.", E_USER_ERROR);
}
for ($i = strlen($iv) - 1; $i >= 0; $i--) {
$salt .= $alphabet[ord($iv[$i]) % 64];
}
return $salt;
}
echo $salt = make_blowfish_salt();
// =>
$password = 'GoogleGuy';
echo $hash = crypt($password, '$2a$10$' . $salt . '$');
// =>
if ($hash === crypt($password, $hash)) {
echo "Authentic!\n";
} else {
echo "Not authentic :(\n";
}
###################################################################
echo $hash = password_hash(
$password,
PASSWORD_BCRYPT,
array('cost' => 10)
);
echo $hash = password_hash(
$password,
PASSWORD_DEFAULT,
);
if (password_verify($password, $hash)) {
echo "Authentic!\n";
} else {
echo "Not authentic :(\n";
}
$hash_info = password_get_info($hash);
var_dump($hash_info);
class Shoppingcart {
private $tax {
get {
if ($this->subTotal < 100) {
$this->tax = 0.07;
} elseif ($this->subTotal >= 100 && $this->subTotal <= 200) {
$this->tax = 0.12;
} else {
$this->tax = 0.15;
}
return sprintf("%.2f%%", $this->tax * 100);
}
set {
$this->total = $this->subTotal * $value;
}
}
private $subTotal, $total;
}
function Squared(int $num) {
return pow($num, 2);
}
/*
The problem is we have no guarantee
that Squared() will always return an int
*/
$num = Squared(Squared(Squared(999)));
$array = array(
array(12,32),
array(61,4),
);
foreach ($array as $coordinates) {
echo "X = $coordinates[0], Y = $coordinates[1]\n";
}
# Output:
// X = 12, Y = 32
// X = 61, Y = 4
###############################################################
foreach ($array as list($x, $y)) {
echo "X = $x, Y = $y";
}
###############################################################
foreach ($arrays as $coordinates) {
list($x, $y) = $coordinates;
echo "X = $x, Y = $y\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment