How-to articles, tricks, and solutions about PHP

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.

PHP new line break in emails

In PHP, you can use the built-in function "nl2br" to convert newline characters to HTML line breaks in emails.

PHP preg replace only allow numbers

You can use the preg_replace() function in PHP to only allow numbers in a string using a regular expression.

PHP Regex to get youtube video ID?

You can use the following regular expression to extract the YouTube video ID from a URL:

PHP remove all characters before specific string

You can use the strstr() function in PHP to remove all characters before a specific string.

php resize image on upload

To resize an image on upload in PHP, you can use the getimagesize function to determine the dimensions of the image, then use the imagecreatetruecolor function to create a new image with the desired dimensions.

PHP returning JSON to JQUERY AJAX CALL

To return JSON from a PHP script to a jQuery AJAX call, you can use the json_encode() function in PHP to convert an array or object into a JSON string.

PHP Sessions across subdomains

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

PHP setcookie "SameSite=Strict"?

The "SameSite=Strict" attribute is a security feature that can be added to a cookie when using the PHP setcookie() function.

PHP sort array alphabetically using a subarray value

In PHP, you can use the "usort" function to sort an array by a specific value of a subarray.

PHP string "contains"

To check if a string contains another string in PHP, you can use the strpos() function.

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

PHP UML Generator

There are several UML generators available for PHP, including:

PHP using Gettext inside <<<EOF string

It is possible to use the gettext function inside a heredoc string in PHP.

php variable in html no other way than: <?php echo $var; ?>

Yes, that is one way to output the value of a PHP variable in HTML.

PHP Warning: Module already loaded in Unknown on line 0

This error message usually indicates that a PHP module is being included more than once in a PHP script.

PHP_SELF vs PATH_INFO vs SCRIPT_NAME vs REQUEST_URI

PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI are all superglobal variables in PHP that provide information about the current URL being requested.

PHP-FPM doesn't write to error log

There are a few things you can try to troubleshoot this issue:

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.

PHP: Check if an array contains all array values from another array

You can use the array_diff function in PHP to compare two arrays and check if the first array contains all of the values from the second array.

PHP: how can I get file creation date?

To get the file creation date in PHP, you can use the filectime() function.

PHP: How to handle <![CDATA[ with SimpleXMLElement?

You can use the addCData() method of the SimpleXMLElement class to add a CDATA section to an XML element.

PHP: HTTP or HTTPS?

In PHP, you can use the $_SERVER['HTTPS'] variable to check whether the current page is being served over HTTPS.

PHP: Limit foreach() statement?

To limit the number of iterations in a foreach loop in PHP, you can use a simple counter variable and a break statement.

PHP: move_uploaded_file(): Unable to move '/

The error message typically occurs when the PHP script is trying to move an uploaded file from its temporary location to a new destination.