How-to articles, tricks, and solutions about PHP

Best practice multi language website

One best practice for implementing a multi-language website in PHP is to use a language detection library such as Text-Language-Detect.

best practice to generate random token for forgot password

A common practice for generating a random token for a "forgot password" feature in PHP is to use the built-in functions random_bytes and bin2hex.

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 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.

break out of if and foreach

To break out of a loop in PHP, you can use the break statement.

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:

Call to undefined function curl_init() even it is enabled in php7

The error message "Call to undefined function curl_init()" typically indicates that the cURL extension for PHP is not installed or is not enabled.

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.

Calling other function in the same controller?

In PHP, you can call a function within the same controller by simply calling the function name followed by parentheses.

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.

Can't access global variable inside function

In PHP, if you want to access a global variable inside a function, you need to use the "global" keyword.

Can't connect to HTTPS site using cURL. Returns 0 length content instead. What can I do?

There are several possible reasons why cURL is returning a 0 length response when trying to connect to an HTTPS site.

Can't use function return value in write context?

In PHP, this error message typically occurs when a function or method that is expected to return a value is being used in a context where a value cannot be written.

can't write image data to path in laravel using the Intervention library

There could be several reasons why you are unable to write image data to a path in Laravel using the Intervention library.

Change composer global path (Windows)

To change the global path for Composer on Windows, you will need to update the system environment variables.

Change Timezone in Lumen or Laravel 5

In Laravel or Lumen, you can set the timezone for your application by setting the timezone option in your config/app.php configuration file.