How-to articles, tricks, and solutions about PHP

In Twig, check if a specific key of an array exists

In Twig, you can use the exists function to check if a specific key of an array exists.

Include constant in string without concatenating

In PHP, you can include a constant in a string without concatenating it by using double quotes (") instead of single quotes (') to define the string.

Increment value in MySQL update query

To increment a value in a MySQL update query using PHP, you can use the += operator.

Insert data into multiple tables using one form

To insert data into multiple tables using one form in PHP, you can use a single SQL statement with multiple INSERT INTO clauses, or you can use multiple individual SQL statements, one for each table.

install php70-gd on ubuntu

To install the php70-gd package on Ubuntu, you will need to have the package manager apt-get installed.

Installing specific laravel version with composer create-project

To install a specific version of Laravel using the Composer create-project command, you can specify the version number after the package name.

Installing the PHP 7 MongoDB Client/Driver?

To install the PHP 7 MongoDB driver, you can use the PECL (PHP Extension Community Library) command-line tool.

Interface or an Abstract Class: which one to use?

An interface defines a set of methods that a class must implement, but does not provide any implementation for those methods.

Invoking a PHP script from a MySQL trigger

It is possible to invoke a PHP script from a MySQL trigger by using the system() function in the trigger's body.

Is there a PHP Sandbox, something like JSFiddle is to JS?

Yes, there are several PHP sandbox options available online.

Is there Java HashMap equivalent in PHP?

In PHP, the equivalent to a Java HashMap would be an associative array.

isset PHP isset($_GET['something']) ? $_GET['something'] : ''

In PHP, isset() is a function that determines whether a variable is set and is not NULL.

Issue reading HTTP request body from a JSON POST in PHP

In PHP, you can read the body of an HTTP request using the file_get_contents('php://input') function.

json_decode to custom class

In PHP, you can use the json_decode() function to convert a JSON string into a PHP object or array.

JSON_ENCODE of multidimensional array giving different results

JSON_ENCODE is a function that converts a PHP data structure, such as an array, into a JSON (JavaScript Object Notation) string.

Laravel - Eloquent or Fluent random row

In Laravel's Eloquent ORM, you can use the inRandomOrder method to retrieve a random row from the database.

laravel - get parameters from http request

In Laravel, you can use the $request object to access parameters from an HTTP request.

Laravel - htmlspecialchars() expects parameter 1 to be string, object given

It looks like you are trying to use the htmlspecialchars() function in Laravel and are encountering an error that says "htmlspecialchars() expects parameter 1 to be string, object given".

Laravel - Model Class not found

If you are receiving an error stating that a specific Model class cannot be found in Laravel, there are a few possible causes:

Laravel - Session store not set on request

It sounds like you are trying to use the Laravel session, but it is not available in the current request.

Laravel - Using (:any?) wildcard for ALL routes?

In Laravel, you can use the (:any?) wildcard to match any URI for a specific route.

Laravel - where less/greater than date syntax

In Laravel, you can use the where method on a query builder or an Eloquent model to filter records based on a date column using less than or greater than operators.

Laravel 5 - Interface is not instantiable

This error message in Laravel 5 typically means that you are trying to instantiate an interface, but interfaces cannot be instantiated because they are only a contract for the methods that a class should implement.

Laravel 5 – Clear Cache in Shared Hosting Server

To clear the cache in Laravel 5, you can use the php artisan cache:clear command.