Hash tables for PHP (#39)
This commit is contained in:
10
05_hash_tables/php/01_price_of_groceries.php
Normal file
10
05_hash_tables/php/01_price_of_groceries.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
$book = [];
|
||||
// an apple costs 67 cents
|
||||
$book['apple'] = 0.67;
|
||||
// milk costs $1.49
|
||||
$book['milk'] = 1.49;
|
||||
$book['avocado'] = 1.49;
|
||||
|
||||
var_dump($book);
|
||||
18
05_hash_tables/php/02_check_voter.php
Normal file
18
05_hash_tables/php/02_check_voter.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
$voted = [];
|
||||
|
||||
function check_voter($name)
|
||||
{
|
||||
global $voted;
|
||||
if (isset($voted[$name])) {
|
||||
print "kick them out!\n";
|
||||
} else {
|
||||
$voted[$name] = true;
|
||||
print "let them vote!\n";
|
||||
}
|
||||
}
|
||||
|
||||
check_voter("tom"); // let them vote!
|
||||
check_voter("mike"); // let them vote!
|
||||
check_voter("mike"); // kick them out!
|
||||
Reference in New Issue
Block a user