Skip to content

Instantly share code, notes, and snippets.

@hochun836
Last active July 4, 2021 23:41
Show Gist options
  • Select an option

  • Save hochun836/a0c3d03285533d5239f935595463ef1f to your computer and use it in GitHub Desktop.

Select an option

Save hochun836/a0c3d03285533d5239f935595463ef1f to your computer and use it in GitHub Desktop.
<?php // <?php is required
// helper
if (!function_exists('separator')) {
function separator($tag = 'TAG')
{
echo '------------ ' . $tag . ' ------------' . PHP_EOL;
}
}
if (!function_exists('cb')) {
function cb(Closure $func)
{
$func();
}
}
if (!function_exists('say_something')) {
function say_something(string $who)
{
return function (string $word) use ($who) {
echo $who . ' say: ' . $word . PHP_EOL;
};
}
}
// exercise
separator('1');
echo 100 . PHP_EOL;
echo 200, PHP_EOL;
separator('2');
$name = 'peter';
$age = 27;
echo 'name: ' . $name . ', age: ' . $age . PHP_EOL;
echo 'name: $name, age: $age' . PHP_EOL;
echo "name: $name, age: $age" . PHP_EOL;
echo $jsonStr = "{'name': '$name', 'age': $age}" . PHP_EOL;
// echo $jsonStr = "{\"name\": \"$name\", \"age\": $age}" . PHP_EOL; // correct format
$arr = json_decode($jsonStr);
var_dump($arr);
separator('3');
$jsonStr = '{"a": 100, "b": 200, "c": 300, "d": 400, "e": 500}';
$arr = json_decode($jsonStr);
var_dump($arr);
var_dump(json_encode($arr));
foreach ($arr as $value) {
echo $value . PHP_EOL;
}
foreach ($arr as $key => $value) {
echo $key . ': ' . $value . PHP_EOL;
}
separator('4');
echo '(1): ' . __FILE__ . PHP_EOL;
echo '(2): ' . __DIR__ . PHP_EOL;
echo '(3): ' . __FUNCTION__ . PHP_EOL;
echo '(4): ' . __CLASS__ . PHP_EOL;
echo '(5): ' . __METHOD__ . PHP_EOL;
echo '(6): ' . __LINE__ . PHP_EOL;
echo '(7): ' . __NAMESPACE__ . PHP_EOL;
separator('5');
cb(function () { // anonymous function
echo 'Test Callback ...' . PHP_EOL;
});
separator('6');
$say = say_something('Peter');
$say('49 + 51 = 100');
separator('7');
if (true) {
$xx = 111;
}
echo $xx . PHP_EOL; // scope of vaiable is split by function {}
// echo $yy; // Undefined variable: yy
separator('8');
echo ('' ?: 200) . PHP_EOL; // php 5.3+ (!empty)
echo ('' ?? 200) . PHP_EOL; // php 7 (!is_null)
// ref: https://clouding.city/php/operator-diff/
// ref: https://clouding.city/php/empty-vs-isset/
// ref: https://www.vnewin.com/php-isset-empty-is-null-difference/
separator('9');
try {
throw new Exception('Test try-catch ...');
} catch (Exception $ex) {
echo 'Caught: ' . $ex->getMessage() . PHP_EOL;
}
separator('10');
$key = 'name';
$std = new stdClass();
$std->{$key} = 'peter';
// $std['age'] = 27; // Uncaught Error: Cannot use object of type stdClass as array
var_dump($std);
separator('11');
$arr = array('key' => 'peter'); // array (string key)
$arr['age'] = 27;
var_dump($arr);
// var_dump(...$arr); // Uncaught Error: Cannot unpack array with string keys
// ref: https://sites.google.com/site/phplearnmark/php/php-zhi-ling-qing-dan/zhen-lie-han-shi/php-array_merge
separator('12');
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // array (number key)
var_dump($arr);
var_dump(...$arr); // php 7.4
// ref: https://stackoverflow.com/questions/45419150/php-spread-syntax-in-array-declaration
separator('13');
$abc = 100;
echo $abc . PHP_EOL;
unset($abc);
//echo $abc; // Undefined variable: abc
echo (isset($abc) ? '$abc isset' : '$abc no isset') . PHP_EOL;
$arr = ['name' => 'peter', 'age' => 27];
var_dump($arr);
unset($arr['name'], $arr['age']);
var_dump($arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment