How-to articles, tricks, and solutions about PHP
Best way to do a PHP switch with multiple values per case?
To get specific attributes from a Laravel Collection, you can use the pluck method.
Best way to document Array options in PHPDoc?
When documenting an array option in PHPDoc, you can use the @var tag followed by the type array and a short description of the option.
Best way to give a variable a default value (simulate Perl ||, ||= )
The best way to give a variable a default value in PHP is to use the ternary operator ?:.
Best way to store passwords in MYSQL database
The best way to store passwords in a MySQL database is to use a one-way hashing algorithm, such as bcrypt or scrypt, to hash the password before storing it in the database.
Best way to test for a variable's existence in PHP; isset() is clearly broken
In PHP, the isset() function is used to check if a variable has been set, meaning that it has been declared and is not equal to NULL.
Block direct access to a file over http but allow php script access
One way to block direct access to a file over HTTP but allow PHP script access is to use a .htaccess file in the same directory as the file.
Calculate age based on date of birth
Here is some sample PHP code that calculates a person's age based on their date of birth:
Calculate difference between two dates using Carbon and Blade
To calculate the difference between two dates using Carbon and Blade, you can use the following steps: