How-to articles, tricks, and solutions about PHP

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.

Change composer global path (Windows)

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

How can I detect if the user is on localhost in PHP?

You can use the $_SERVER['HTTP_HOST'] variable to detect if the user is on localhost in PHP.

How can I view/open a word document in my browser using with PHP or HTML

You can use the PHP function readfile() to read the contents of a Word document and then use the appropriate headers to prompt the browser to open or download the file.

PHP: Best way to extract text within parenthesis?

There are a few different ways to extract text within parentheses in PHP, depending on the specific requirements of your use case.

Check if all values in array are the same

In PHP, you can use the array_unique function to check if all values in an array are the same.

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.

Getting ’ instead of an apostrophe(') in PHP

This can occur if your PHP code is using the ISO-8859-1 character set instead of UTF-8.

Most efficient way to search for object in an array by a specific property's value

In PHP, you can use the built-in function array_filter() to filter the array by a specific property's value.

Automatic Logout after 15 minutes of inactive in php

To automatically log out a user after 15 minutes of inactivity in PHP, you can use the session mechanism provided by PHP.

How to add text to an image with PHP GD library

Here is an example of how you can add text to an image using the PHP GD library:

How to run a PHP function from an HTML form?

To run a PHP function from an HTML form, you will need to create an HTML form with the desired inputs, such as text fields and submit buttons.

Convert plain text URLs into HTML hyperlinks in PHP

You can use the preg_replace() function in PHP to convert plain text URLs into HTML hyperlinks.

file_put_contents and a new line help

file_put_contents is a PHP function that writes a string to a file.

How to match whitespace, carriage return and line feed using regular expression in PHP?

In PHP, you can match whitespace, carriage return, and line feed using the following regular expression:

PHP's file_exists() will not work for me?

The file_exists() function in PHP can be used to check if a file exists on the file system.

setcookie, Cannot modify header information - headers already sent

The error message "Cannot modify header information - headers already sent" typically occurs when a PHP script attempts to send a cookie or redirect the user's browser to a different page, but some output has already been sent to the browser.

Laravel storage link won't work on production

There are several reasons why the Laravel storage link may not be working on production.

How to make a countdown using PHP

One way to make a countdown using PHP is to use the time() function, which returns the current timestamp, and subtract a future timestamp (representing the end date and time of the countdown) from it.

Converting Varchar Value to Integer/Decimal Value in SQL Server

In SQL Server, you can convert a varchar value to an integer or decimal value using the CAST or CONVERT function.

How do I see the actual XML generated by PHP SOAP Client Class?

To see the actual XML generated by the PHP SOAP Client class, you can use the __getLastRequest() method.

Create Array from Foreach

To create an array from a foreach loop in PHP, you can use the array push function array_push() inside the loop.

Nullable return types in PHP7

In PHP7, nullable return types were introduced, allowing a function to return either a specific type or null.

Divide integer and get integer value

In PHP, you can divide two integers using the forward slash (/) operator.