{"id":17168719,"url":"https://github.com/sanix-darker/php_learn","last_synced_at":"2025-03-24T18:43:41.087Z","repository":{"id":243215267,"uuid":"802661544","full_name":"Sanix-Darker/php_learn","owner":"Sanix-Darker","description":"php learn","archived":false,"fork":false,"pushed_at":"2024-06-07T11:52:17.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-29T23:27:53.682Z","etag":null,"topics":["learning","php","tutorial"],"latest_commit_sha":null,"homepage":"https://sanix-darker.github.io/php_learn/php.html","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sanix-Darker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-18T22:58:31.000Z","updated_at":"2024-06-07T11:52:20.000Z","dependencies_parsed_at":"2024-06-07T11:56:17.567Z","dependency_job_id":"7de8868d-3ee9-43a9-af8c-285671693927","html_url":"https://github.com/Sanix-Darker/php_learn","commit_stats":null,"previous_names":["sanix-darker/php_learn"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sanix-Darker%2Fphp_learn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sanix-Darker%2Fphp_learn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sanix-Darker%2Fphp_learn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sanix-Darker%2Fphp_learn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sanix-Darker","download_url":"https://codeload.github.com/Sanix-Darker/php_learn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245330880,"owners_count":20597854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["learning","php","tutorial"],"created_at":"2024-10-14T23:12:40.675Z","updated_at":"2025-03-24T18:43:41.061Z","avatar_url":"https://github.com/Sanix-Darker.png","language":"HTML","readme":"## PHP LEARN\n\nYeah... each statement need to end with a comma \";\".... BOOOOO.\n\n## LEARN MAP\n\n![map](./php.svg)\n\n[CHECK THE INTERACTIVE MAP](https://sanix-darker.github.io/php_learn/php.html)\n\n## CONCEPTS\n\n### PRINTERS\n\n```php\n\u003c?\necho \"ok\", 12# can take multiple items, but only print item that can converted to string\nprint \"Hello\";# only one item\nprint(\"Hello\");# only one item\n\nvar_export(\"something\");\n```\n\n### DATA TYPES\n\n```php\n\u003c?\n// constants\ndefine(\"EXAMPLE_CONSTANT\", 12);\n\n// Variables and data types :\n\n$str = \"Hello\";\n$bool = true; // or false\n$int = 12;\n$float = 12.12;\n\n$is_empty = NULL;\n\n$arr = array('Hello', 12, 'there', false);\n# to add values to the array (\n    - array_push($arr, \"item\", \"item 2\"); # to add new items\n    - in_array($var, \"value\"); # to check if a value is in array\n    - array_map(function($val) { return \"this\" }) # apply a method to all items in the arr.\n       \u003e $arr_val = [\"hello\", \"this\", \"and\", \"that\"];\n       \u003e $arr_val_Upper = array_map(function($val) {\n       \u003e     return strtoupper($val);\n       \u003e }, $arr_val);\n       \u003e print_r($arr_val_Upper);\n    - array_filter($values, fn($nn) =\u003e return $condition); # to filter values based on a condition\n    - array_reduce()\n       \u003e print_r(\n       \u003e     array_reduce([\"this\", \"and\", \"that\"], function($a, $b) {\n       \u003e         return \"{$a} - {$b}\";\n       \u003e     })\n       \u003e );\n    - array_pop($arr); # to remove the last added item\n    - array_shift($arr); # to remove the first item in the array\n    - array_unshift($arr, \"prev_value\"); # to preppend the array with a new value.\n    - unset($arr[3]); # to remove the item [3] from the array.\n    - array_chunk($arr, 3); # to split an array into multiple small chunks\n    - $arr = array_reverse($arr); # to shift values\n    - count($arr); # to count values inside the array\n    - # to merge two arrays:\n        - array_merge($arr1, $arr2);\n        - [...$arr0, ...$arr2];\n)\nvar_dump($arr); //to print the whole content\n\n$hash_map = array(\n    'x' =\u003e 12, 'y' =\u003e 33, 'z' =\u003e $arr\n);\nvar_dump($hash_map); // we can also use print_r, it's same but less infos.\n// to loop inside an hasmap (assotiative array);\nforeach ($hash_map as $key =\u003e $value){\n    echo $key.\" ---\u003e \".$value\n}\n// get keys\narray_keys($hash_map);\n// get values\narray_values($hash_map);\n// we can flip keys and values too:\n$new_flipped_hashmap_arr = array_flip($hash_map);\n\n// To combine two arrays in a way we have keys -\u003e values\n// we could use array_combine();\n$final_hash_map = array_combine($arr_keys, $arr_values);\n// we can define ranges like in python\n$range_10 = range(10);\n\n// Resources Data type (holds a resources).\n?\u003e\n```\n\n### CONDITIONS\n\n- if / else:\n\n```php\n\u003c?\n// Note:\n// == Equal to\n// === Identical to\n\n// != Not Equal to\n// !== Not Identical to\nif ($user == \"this\" AND $pass == \"xxx\") { // AND (\u0026\u0026) | OR (||)\n    echo \"this\";\n }else{\n    echo \"that\";\n}\n```\n\n- switch /case:\n\n```php\n\u003c?\nconst name_r = \"Hlllo\";\nswitch (strlen(name_r)) {\n    case 5:\n        echo \"IT's 5\";\n        break;\n    default:\n        echo \"OUPS !!!\";\n        break;\n}\n```\n\n- ternary:\n\n```php\n\u003c?\necho ($content_radian \u003e 10) ? \"is upper than 10\" : \"is lower than 10\";\n\n// Usefull to check the variable and the ternary evaluation directly.\necho $post[0] ?? \"NULL\";\n```\n\n### SESSION VARIABLES\n\nGlobal Session variables :\n\n- `$_GET`: all query params.\n- `$_ENV`: for env vars.\n- `$_FILES`: contains infos about files uploaded.\n- `$_POST`: all POST form params.\n- `$_REQUEST`: both.\n- `$_SESSION`: session params but needed a `session_start` to be initiated.\n    NOTE: to kill a session, it's `session_destroy();`\n- `$_COOKIES`: cookies values.\n- `$_SERVER`: entries created by the webserver.\n\n\n### LOOP\n\n- foreach\n```php\n\u003c?\n$items = ['this', 10, 'that']; // array('', ...) works too\n\nforeach ($items as $item){\n    echo $item;\n}\n```\n- while\n\n```php\n\u003c?\n$i = 0;\nwhile($i \u003c 10){\n    // do something\n    echo \"count - \".$i;\n    $i += 1;\n}\n```\n\n- do/while (execute first before checking the condition).\n```php\n\u003c?\n$i = 0;\ndo {\n    echo \"--{$i}\";\n    $i -= 1;\n}while($i \u003e 3);\n```\n\n- for\n```php\n\u003c?\nfor ($i = 0; $i \u003c 10 \u0026i ++;){\n    echo $i;\n}\n```\n## BUILTIN FN\n\n```php\n\u003c?\nrand(1, 100); # for random value in that range\n# The list below is pretty obvious i guess...\n\nabs(12.3);\nfloor(12.3);\nceil(12.3);\nround(12.3);\nsqrt(9);\npow(3, 4);\n\nmax(23, 4, 1, 55);\nmin(23, 4, 1, 55);\n\npi(23, 4, 1, 55);\n\n# more practical\nisset(); // true if var declared and not null\nempty(); // true if not declared or false or null or \"\"\n\n# date\necho date(\"H\");\n\n// We can filter input _POST to prevent XSS injections\n// the big advantages for this is to not have to use isset()\n//  each times\n$name = filter_input(\n    INPUT_POST, # this is know by php itself\n    \"name\",\n    FILTER_SANITIZE_SPECIAL_CHARS # this too...(there is multiple tyhpe of FILTER_... mode)\n);\necho $name;\n\n// manipulating data\necho var_dump(json_encode($val));\n\n// htmlspecialchars\n// to prevent also xss injections\necho htmlspecialchars($tring);\n```\n\n### FORMATING\n\n```php\n\u003c?\nuser=\"dk\";\necho \"{$user} is connected !\";\n\n$amount = 20.23;\n// we can also escape characters.\necho \"{$user} owned me \\${$amount}\";\n\n// formating in a C way\nprintf(\"%s is %d years old and has $%f\", \"sanix\", 20, 12);\n```\n\n### FILE HANDLING\n\n```php\n\u003c?\n$file = \"path.txt\"\n\nif (file_exists($file)){\n    $handle = fopen($file, 'r');\n    $contents = fread($handle, filesize($file));\n    fclose($handle);\n\n    echo $contents;\n}else{\n    $handle = fopen($file, 'w');\n    fwrite($handle, $contents);\n    fclose($handle);\n}\n```\n\n### EXCEPTION\n\n```php\n\u003c?\n\nfunction inverse(int $val): float{\n    if (!$val){\n        throw new Exception('val need to be set');\n    }\n    return 1/$val;\n}\n\ntry{\n    echo inverse(3);\n    echo \"\u003cbr\u003e\";\n    echo inverse(0);\n}catch(Exception $e){\n    echo \"Caught... this and that. {$e}\";\n}finally{\n    echo \"always executed.\";\n}\n```\n\n### OOP\n```php\n\u003c?\nclass User{\n    public string $name;\n    public int $age;\n    public bool $gender; //true M, false F\n\n    public string $email; // Can not be access from outside\n\n    private $account;\n    protected $bank_amount;\n\n    public function __construct(string $name, int $age, bool $gender, string $email){\n        $this-\u003ename = $name;\n        $this-\u003eage = $age;\n        $this-\u003egender = $gender;\n        $this-\u003eemail = $email;\n    }\n\n    public function get_email(): string {\n        return $this-\u003eemail;\n    }\n\n    public function set_email(string $given_email): void{\n        $this-\u003eemail = $given_email;\n    }\n}\n\n$usr = new User(\"dk\", 20, true, \"a@b.c\");\necho $usr-\u003eget_email();\n// inheritence\n//\nclass Employee extends User {\n    public mixed $custom;\n\n    public function __construct(string $name, int $age, bool $gender, string $email, mixed $custom){\n        parent::__construct($name, $age, $gender, $email);\n        // new props\n        $this-\u003ecustom = $custom;\n    }\n\n    public function get_custom(): mixed{\n        return $this-\u003ecustom;\n    }\n}\n$eee = new Employee(\"sk\", 34, false, \"t@b.c\", [\"a\", \"b\"]);\necho \"\u003cbr\u003e\";\nprint_r($eee-\u003eget_custom());\n```\n\n### EXTRAS\n\n```php\n\u003c?\n// to include a page to another one\ninclude(\"this.php\");\n\n# or // can be used to define comments\n\n# size of a string var\nstrlen($var); // to get size of a string\nstrtolower($var); // to lower string\nstrtoupper($var); // to upper string\ntrim($var); // to remove spaces surounding string\n// to add a padding of characters after the string\nstr_pad($var, 10, \"-\"); // to lower string\nstr_replace(\"o\", \"a\", \"Hello Warld!\");\nstrrev($var); // to reverse the string\nstr_shuffle($var); // to shuffle characters in strings\nstrcmp($var1, $var2); // to compare two strings\n\nstrpos($var, \"a\"); // to the theindex of the first occurence of \"a\" in $var.\nsubstr($var, 10); // string substract\nsubstr($var, 2, 10); // string substract with a start/end indexes\n\nucwords($string); // upper each first character for each words.\n\nstr_start_with(\"value\", \"v\"); // check if a string start with something specific.\nstr_end_with(); // same loggic but with end.\n\nexplode(\" \", \"a b c\"); // like a split and return an array.\nimplode(\"-\", array(\"a\", \"b\")); // like a join\n\n# hashs method\n$hh = password_hash(\"value\", PASSWORD_DEFAULT);\n# and to check/verify\npassword_verify(\"value\", $the_hash_version);\n\n# pass a callback to a method (with default params)\nfunction print_it($callback, $args=array()){\n    $callback($args);\n}\nprint_it(\n    'print_r', # to pass a callback, it should be defined in params as string.\n    array(\"_GET: \" =\u003e $_GET),\n);\n\n# to deal with checkboxes in php, we need to set the same var that will\n# be identify as an array in the backend side.\n?\u003e\n\u003cform action=\"index.php\" method=\"post\"\u003e\n    \u003cinput type=\"checkbox\" name=\"foods[]\" /\u003e\n    \u003cinput type=\"checkbox\" name=\"foods[]\" /\u003e\n    \u003cinput type=\"checkbox\" name=\"foods[]\" /\u003e\n    \u003cinput type=\"checkbox\" name=\"foods[]\" /\u003e\n\u003c/form\u003e\n\u003c!-- in the php side --\u003e\n\u003c?\n$foods = $_POST[\"foods\"];\n# then loop on it\nforeach ($foods as $food){\n    echo \"\u003e\u003e\".$food;\n}\n\n// cookies\nsetcookie(\"key\", \"value\", time() + (100), \"/path\");\nsetcookie(\"this\", \"that\", time() + 100, \"/path/to\");\n// those are stored in $_COOKIES;\n// as assotciative array.\n\n// session\n// same principle for $_SESSION[\"values\"]\n\n# to get the curret php file\necho $_SERVER[\"PHP_SELF\"];\n// the request method made\necho $_SERVER[\"REQUEST_METHOD\"]; # \"POST\", \"GET\"\n\n// globals var used in method context.\n$this_is_global = 1;\nfunction custom_fn(){\n    global $this_is_global;\n    echo $this_is_global;\n}\ncustom_fn();\n\n// arrow function\n// anonymous function assign to variable\n$arrow_fn = fn (int $a, string $b) : int =\u003e {\n    return strlen($b) + $a;\n};\necho $arrow_fn(12, \"this is a test\");\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanix-darker%2Fphp_learn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsanix-darker%2Fphp_learn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanix-darker%2Fphp_learn/lists"}