Created
November 9, 2012 18:27
-
-
Save AlexTiTanium/4047363 to your computer and use it in GitHub Desktop.
PHP 5.5 new
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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