Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Define $output on multiple $lines
* Use Herodoc syntax.
*
* output =
* She said "This is John's test"
* on multiple rows
*/
<?php
/**
* Define getMaxValue() method
* Use static syntax
*/
class MyClass {
const MAX_VALUE = 100;
@minte9
minte9 / index.php
Last active April 14, 2021 07:24
Php / Basics / Reference
<?php
/**
* array_map()
*
* Round up array values
* Use array_map() with built-in ceil functions
* The result array $mapped will be [2, 3, 4]
*/
<?php
/**
* Define class constructor and ...
* Display the constructor method name
*/
$obj = new A();
class A
{
@minte9
minte9 / index.php
Last active April 14, 2021 08:19
Php / Basics / Integers / Decimal 2 Ocatal - https://www.minte9.com/php/basics-integers-13
<?php
/**
* Compose $m octal value using myFunc() recursively
* Use reminder and quontient values
*
* 97 (decimal) = 141 (octal)
* 97 % 8 => reminder 1
* 12 % 8 => reminder 4
* 1 % 8 => reminder 1
<?php
/**
* Given the users $role and access $rights ...
* Check if Mike has admin rights and downgrade him
* Wrong approach:
* if ($is_admin && $can_edit_articles ...
*/
$rights = (object) ["read"=>1, "write"=>2, "readwrite"=>16, "admin"=> 32];
$role = (object) ["jim"=>96, "mike"=>32];
<?php
/**
* Replace
* {b} abc {/b} with:
* <b> abc </b>
* Use preg_replace() function
*/
$str = "{b} abc {/b}";
# Write a function convert() that returns
# days, hours, minutes, seconds since UTC
# The time module provides a function, also named time,
# that returns the current GMT in “the epoch”
import time
today = time.time()
# SOLUTION
@minte9
minte9 / cypher.py
Last active June 2, 2021 17:36
Python / Language / Strings
# A Caesar cypher is a weak form on encryption
# It involves "rotating" each letter by a number (shift it through the alphabet)
# A rotated by 3 is D; Z rotated by 1 is A
# In a SF movie the computer is called HAL, which is IBM rotated by -1
# Write a function rotate_word()
# Use built-in functions ord (char to code_number), chr (codes to char)
# SOLUTION
@minte9
minte9 / consecutive.py
Last active June 5, 2021 14:59
Python / Language / Strings
# Get the words with three consecutive double letters
# https://github.com/AllenDowney/ThinkPython2/blob/master/code/words.txt
def has3_2consecutive(word):
i = 0; count = 0
while i < len(word) - 1:
if word[i] == word[i+1]:
count = count + 1
if count == 3:
return True