How-to articles, tricks, and solutions about PHP

Where to find composer's global packages?

Composer stores global packages in the "vendor/bin" directory within your system's home directory.

"Database query failed: Data truncated for column 'column_name' at row 1

This error message is indicating that a database query has failed due to data being too large to fit into a specific column in the database table.

PDOException: SQLSTATE[HY000] [2002] No such file or directory

This error message typically indicates that there is a problem connecting to the database using PHP's PDO (PHP Data Objects) library.

Laravel Socialite: InvalidStateException

The InvalidStateException is thrown by Laravel's Socialite package when the state parameter provided in the OAuth redirect URL does not match the one stored in the session.

PHP MYSQL UPDATE if Exist or INSERT if not?

You can use the ON DUPLICATE KEY UPDATE statement in your MySQL query when using PHP's mysqli or PDO libraries to accomplish this.

install php70-gd on ubuntu

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

Create a Laravel Request object on the fly

In Laravel, you can create a Request object on the fly by using the create method of the Request facade.

PHP cURL how to add the User Agent value OR overcome the Servers blocking cURL requests?

You can add a custom User Agent value to a cURL request in PHP using the CURLOPT_USERAGENT option.

How to add 5 minutes to current datetime on php < 5.3

You can use the strtotime function in PHP to add a certain number of minutes to a date and time.

How to get all rows (soft deleted too) from a table in Laravel?

In Laravel, you can use the withTrashed() method to retrieve all the rows, including the soft deleted ones, from a table.

How can I use PHP to dynamically publish an ical file to be read by Google Calendar?

Here's an example of how you can use the iCalcreator library to create an iCal file and output it for download:

Example of how to use bind_result vs get_result

The mysqli_stmt::bind_result() method binds variables to a prepared statement for result storage.

Alternative to file_get_contents?

file_get_contents() is a built-in PHP function that is commonly used to read the contents of a file into a string.

PHP IF statement for Boolean values: $var === true vs $var

In PHP, a variable can be evaluated as a Boolean value in an if statement without the need to explicitly compare it to true or false.

Saving file into a prespecified directory using FPDF

To save a file using FPDF, you can use the "Output" method.

Delete particular word from string

In PHP, you can use the str_replace function to delete a particular word from a string.

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

Remote file size without downloading file

In PHP, you can use the get_headers() function to retrieve the headers of a remote file.

file_get_contents behind a proxy?

The file_get_contents function in PHP can be used to retrieve the contents of a file from a remote server.

PHPExcel and Text Wrapping

PHPExcel is a library for working with Microsoft Excel files in PHP.

Remove a child with a specific attribute, in SimpleXML for PHP

You can remove a child element with a specific attribute in SimpleXML for PHP using the xpath() function.

Ignore case sensitivity when comparing strings in PHP

In PHP, you can use the strcasecmp() function to compare two strings and ignore case sensitivity.

PHP foreach with Nested Array?

To loop through a nested array in PHP using a foreach loop, you can use nested foreach loops.

error in your SQL syntax; check the manual that corresponds to your MySQL server

This error message is indicating that there is a problem with the SQL syntax used in the query.

Executing multiple SQL queries in one statement with PHP

In PHP, you can execute multiple SQL queries in one statement by using the mysqli_multi_query() function.