How-to articles, tricks, and solutions about PHP

CHECK PHP SESSION - ISSET($SESSION - IS NOT WORKING

It sounds like you are having an issue with the isset() function in PHP not working properly when checking for a session variable.

PHP Sessions across subdomains

PHP sessions can be shared across subdomains by setting the session cookie's domain to the top-level domain.

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.

PHP CLI won't log errors

There are a few things that can cause errors to not be logged when running PHP from the command line interface (CLI).

How to write own DD() function same as laravel?

To create your own version of the Laravel DD() function, you can create a new function in your codebase and have it take in any number of arguments.

How to send HTTPS posts using php

To send an HTTPS POST request using PHP, you can use the curl extension.

How to get the OS on which PHP is running?

You can use the php_uname() function to get the operating system on which PHP is running.

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.

Limit array to 5 items

You can use the "array_slice" function in PHP to limit an array to a specific number of items.

PHP subtract 1 month from date formatted with date ('m-Y')

You can use the DateTime object in PHP to subtract one month from a date formatted with date('m-Y').

How to assign an array within a smarty template file?

In a Smarty template file, you can assign an array to a variable using the assign function.

Add 30 days to date

In PHP, you can add 30 days to a date by using the DateTime class and the modify() method.

Submit html table data with via form (post)

Here is an example of how to submit an HTML table data via a form (using the POST method) using PHP:

Creating anonymous objects in php

In PHP, anonymous objects can be created using the new class syntax.

Does UTC observe daylight saving time?

UTC (Coordinated Universal Time) is a standardized time that does not observe daylight saving time (DST).

How to disable output buffering in PHP

In PHP, you can disable output buffering by calling the ob_end_flush() function or flush() function.

PHP: Storing 'objects' inside the $_SESSION

In PHP, the $_SESSION superglobal is used to store data that needs to persist across multiple page requests.

How to create new property dynamically

In PHP, you can create a new property for an object dynamically by using the magic method __set.

search a php array for partial string match

You can use the array_filter() function in PHP to search through an array and return only the elements that contain a certain string.

ZIP all files in directory and download generated .zip

Here is an example of how you can use PHP to create a ZIP archive of all files in a directory and then prompt the user to download the generated ZIP file:

How to fix 'Creating default object from empty value' warning in PHP?

This error typically occurs when a variable is accessed as an object, but it has not been initialized or set to an object.

How to count and group by in yii2

In Yii2, you can use the count() and groupBy() methods in the query builder to count and group the results of a query, respectively.

PHP header location-redirect doesn't work - why?

There are several reasons why a PHP header location redirect may not work:

How to display woocommerce sale price or regular price if there is no sale price

In WooCommerce, you can display the sale price or regular price of a product using the following code snippet:

Allow only [a-z][A-Z][0-9] in string using PHP

You can use the preg_replace function in PHP to remove any characters that do not match the specified pattern.