How-to articles, tricks, and solutions about PHP
Best practices to test protected methods with PHPUnit
There are a few ways you can test protected methods using PHPUnit:
Best solution to protect PHP code without encryption
One solution to protect PHP code without encryption is to use code obfuscation.
Best way to check if mysql_query returned any results?
To check if a MySQL query returned any results, you can use the mysqli_num_rows function.
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 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 ?:.
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:
Calling a PHP function by onclick event
To call a PHP function using an onclick event, you will need to use a little bit of JavaScript.
Can a class extend both a class and implement an Interface
Yes, a class in PHP can extend another class and implement one or more interfaces at the same time.
Can we pass an array as parameter in any function in PHP?
Yes, you can pass an array as a parameter to a function in PHP.