php

#define php: \ I-----------------------------------------------\ I-----------------------------------------------\ I-----------------------------------------------\ I /$$$$$$$ /$$ /$$ /$$$$$$$ \ I | $$__ $$| $$ | $$ | $$__ $$ \ I | $$ \ $$| $$ | $$ | $$ \ $$ \ I | $$$$$$$/| $$$$$$$$ | $$$$$$$/ \ I | $$____/ | $$__ $$ | $$____/ \ I | $$ | $$ | $$ | $$ \ I | $$ | $$ | $$ | $$ \ I |__/ (php)|__/ |__/ |__/ \ I-----------------------------------------------\ I-----------------------------------------------\ I-----------------------------------------------I "PHP Hypertext Preprocessor"/"PHP Hates Programmers"/"Programmable Hyperlinked Pasta" • server side language • whitespace insensitive • semicolons are required at the end of every statement; • often described as "a thin wrapper around C", which is partially true, but without any upsides of C • somewhat C-like • nobody understands how this language is still in use, despite that every building block is flawed; I personally believe academia is to blame for still teaching it • unless you are absolutely required to work with PHP, use Perl instead • whats baffling is that Perl is older than PHP https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/ • shipping with documentation is considered bad practice (https://github.com/PHPMailer/PHPMailer/issues/919) • hackable, NOT in the MIT sense; it is single handedly responsible for the wild-west phase of web application hacking during the 2000's • anything it does, Perl can do better • the interfaces of the standard library are horrific — the type system is not reliable at all: • if you have to have "===", that automatically indicates that the type system is a joke <?php echo md5('240610708') . "\n"; # Out: 0e462097431906509019562988736854 echo md5('QNKCDZO') . "\n"; # Out: 0e830400451993494058024219903391 echo (md5('240610708') == md5('QNKCDZO')) . "\n"; # Out: 1 (true) # both hashes start with '0e', which results in # them being treated as scientific notation # zero times whatever is zero ?> <?php $a = "1a"; echo $a+1 . "\n"; # Out: 2 echo ++$a . "\n"; # Out: 1b # Note: its not even a hexadecimal thing ?> PROGRAMS: php [options] <file> : PHP interpreter — S <addr>:<port> : launch the builtin webserver; it is significantly more pleasant, than trying to develop using a server daemon, because it provides invaluable diagnostics; however it still falls flat compared to CGI { $ php -S 127.0.0.1:8000 } php-config [options] : print information regarding a PHP installation phar : operates on PHP's archive format; see BELOW PHAR:"PHp ARchive" • an archive format, for the PHP infrastructure • some libraries do ship in phar • in reality its used for packaging multiple PHP scripts as a stand-alone executable • they are not supposed to be ever extracted • PHP's documentation compares it to tar-s and zip-s, yet it has absolutely nothing to do with conventional archive formats, and misunderstanding this is a major safety concern: " \ A Phar's stub is a simple PHP file. [...] \ There are no restrictions on the contents of a Phar stub, \ except for the requirement that it conclude with __HALT_COMPILER();. \ " - https://www.php.net/manual/en/phar.fileformat.stub.php — the documentation is rather comedic: +--------------------------------------------+ | Official Feature matrix (Partial) | +-------------------------+------+-----+-----+ | Feature | Phar | Tar | Zip | +-------------------------+------+-----+-----+ | Can be executed without | Yes | No | No | | the Phar Extension | | | | | Can be created/modified | No | Yes | Yes | | even if phar.readonly=1| | | | +-------------------------+------+-----+-----+ COMMENTING: # || // : single line comment /* [...] */ : multi line comment VARIABLES: • variable names must begin with a letter or underscore character • every variable name starts with a '$' • therefor if you wanna use '$' as a char, escape it ('\$') ○ types: • int • double • bool • string //no such a thing here as a char type — array array([values]+) : returns array [(values)] : short-hand alias of the ABOVE • associative • class objects • null : special type that only has one value: NULL. • resources : special variables that hold references to resources external to PHP (such as database connections) Setting: • what other languages would call definition or (wrongly) construction (const) $[name] = [value]; LOGIC: if : same as in C LOOPS: while : same as in C do [...] while : same as in C for : same as in C foreach(<array> as <var>){[...]} : conventional for-each loop; <var> will take up the values of the elements of <array> Broken_up: • php allows loops to start and end within different php tags • anything between a broken up loop start and end is considered conditional (including html) [loop]: : broken up loop start end[loop] : broken up loop end <?php # repeating a html span 10 times with php for($i = 0; $i < 10; $i++): ?> <span>Some text<span> <?php endfor; ?> OPERATORS: • arithmetics inherited from C echo <string> : echoes (prints) <string>; faster than "print" print <string> : prints <string>; do { print <<<END } to add new line INCLUSION: include [path] : insert all text from [path] include_once [path] : insert all text from [path] if it wasnt done-so before in the current document require [path] : insert all text from [path]; die if [path] doesnt exist require_once [path] : insert all text from [path] if it wasnt done-so before in the current document; die if [path] doesnt exist BUILTINS: • i dont know by what fucked logic the std is organized, but quite frankly i dont actually give a shit EXECUTION: exit() : the only line every php program should contain die() : equivalent to exit() VARIABLE_MANAGEMENT: isset([var]) : returns whether [var] has been defined unset([var]) : delete-s [var] IO: var_dump([var]) : prints information regarding [var] and its value print_r([array]) : prints all elements of array [array] get_object_vars([object]) : returns an associative array of non static member variables of [object] error_log([string]) : log [string] to the execution logs; this could be stdout or a server log Filesystem_operations: basename(<string>) : supposing <string> is a path to a file, it will return the file name { "/asd/das.php" => "das.php" } dirname(<string>) : supposing <string> is a path to a file, it will return the path name { "/asd/das.php" => "/asd" } file_exists(<string>) : checks whether <string> dir/file exists; returns bool is_file(<string>) : checks whether <string> file exists; returns bool realpath(<string>) : returns absulute path of <string> file filesize([file]) : returns file size mkdir([name]) : makes dir with [name] copy([file1], [file2]) : copies [file1] as [file2] rename([file1], [file2]) : renames [file1] to [file2] unlink([file]) : deletes [file] file_get_contents([file]) : returns [file]s content File_operations: fopen([FILE], [mode]) : returns handle to [file] with intent of [mode] modes: 'r' : read 'r+' : read and write 'w' : write 'w+' : read and write; if doesnt exist create it [...] //the rest are only useful in very specific cases fread([file handle], <int>) : returns <int> much of [file]s content fwrite([file handle], <string>) : fclose([file hanle]) : should always be called when done working with [file] file_get_contents([path]) : returns the a string constructed from the text contents of file [path] INT: round() STRING: strlen(<string>) : returns the number chars (maybe bytes ?!) in <string> strtolower(<string>) : returns <string> converted to all lower case explode([string-1], [string-2]) : returns an array constructed from [string-2] split at every occurence of [string-1] substr(<string>, [int-1], [int-2]) : returns [int-2] chars from position [int-1] of <string> is_numeric(<string>) : returns whether <string> is a valid number ARRAY: array_key_exists(<string>, [array]) : returns weather associative array [array] has a member with the key <string> key_exists(<string>, [array]) : alias of the ABOVE in_array([val], [array]) array_push([array], [value]) count([array]) REGEX: preg_match([regex], <string>) : returns whether <string> matches regex string [regex] SESSION: session_start() session_destroy() HTML: header(<string>) : sets document header to <string>; commonly used to redirect by setting: "Location: [page]" HTML_DOC: • php can edit html files as a struct-ed document, not just plain text • modelled after javascript Classes: class DOMDocument; Member_functions: loadHtml([html]) : properly constructs DOMDocument Functions: getElementsByTagName() getElementsByClassName() Dom_element: textContent item(<int>) : returns <int>th child as dom element getAttribute(<string>) MYSQL: class mysqli([host], [user], [password], [db]) • mysql will not allow php to connect as root and rightfully so Member_variables: bool connect_error : whether a database connection error was encountered; should be called after construction Member_Functions: query([SQL]) : performs SQL query; returns: false - on error true - on success mysqli_result - if SELECT, SHOW, DESCRIBE or EXPLAIN query was passed close() class mysqli_result fetch_assoc()

laravel

#define laravel:: \ I-------------------------------------------\ I _ _ \ I | | | | \ I | | __ _ _ __ __ ___ _____| | \ I | | / _` | '__/ _` \ \ / / _ \ | \ I | |___| (_| | | | (_| |\ V / __/ | \ I \_____/\__,_|_| \__,_| \_/ \___|_| \ I-------------------------------------------I • php framework (for web services) • it employs an in-project name-mangling scheme, which introduces all kinds of problems { https://www.reddit.com/r/programminghorror/comments/1jebkng/laravels_syntax_hijacking_forced_me_to_refactor/ } • the complexity they have accumulated is ridiculous # "Empty" project in Laravel $ date Wed Jul 3 10:36:17 AM CEST 2024 $ composer.phar create-project --prefer-dist laravel/laravel projectName # ... $ tree projectName/ # ... 1239 directories, 7975 files $ statAlias projectName/ # ... 77M projectName CLI: composer (<options>) <action> : dependency manager <action>: <verb> (<options>) (<args>) create-project --prefer-dist laravel/laravel <name> php artisan <action> migrate:<specific> rollback reset status serve make:<subject> <name> controller view migration seeder command Structure: /routes/web.php : used for defining routes /resources/ ├── css ├── js └── views CRUD: Classes: • models member_functions: all() : returns all records create(<request>) find(<id>) delete(<id>) delete() Controllers: { public function index() { $data = ['example' => 'value']; return view('index', $data); } } Routes: use Illuminate\Support\Facades\Route; Route::<method>(<route: string>, [<controller: class>, <function: string>]) { Route::post("/example", [ExampleController::class, "myMemberFunction"])->name("example_name"); } Templates: • view *.blade.php <!DOCTYPE html> <html> <head> <title>{{ $pageTitle ?? 'Default Title' }}</title> </head> <body> <h1>{{ isset($header) ? $header : 'Default Header' }}</h1> <div> @if($showContent) <p>Welcome to the content section!</p> @else <p>Content is not available.</p> @endif <ul> @foreach($items as $item) <li>{{ $item }}</li> @endforeach </ul> </div> <footer> <p>© {{ date('Y') }} My Laravel App</p> </footer> </body> </html> Migrations: up() down() Seeders: { public function run() { DB::table('admins')->insert([ 'name' => 'Anon Anonson', ]); // Add more seed data as needed } } Forms: { <form action="/your-route" method="post"> // action="{{ route('example.route') }}" @csrf <!-- ... --> </form> } Authentication: class User • builtin class Auth login(<...>) logout()