How-to articles, tricks, and solutions about PHP

PHP cURL HTTP CODE return 0

If cURL is returning a HTTP code of 0, it usually indicates that cURL was unable to communicate with the server.

Calling a PHP function by onclick event

To call a PHP function using an onclick event, you will need to use a little bit of JavaScript.

How to Truncate a string in PHP to the word closest to a certain number of characters?

To truncate a string in PHP to the word closest to a certain number of characters, you can use the following code:

$_SERVER['HTTP_REFERER'] missing

The $_SERVER['HTTP_REFERER'] variable is a server-side variable that contains the URL of the page that linked to the current page.

submit a form in a new tab

To open a form submission in a new tab, you can use the target attribute of the form element and set it to _blank.

How do you enable mod_rewrite on any OS?

To enable mod_rewrite on a PHP server, you will need to make sure that the Apache mod_rewrite module is installed and enabled.

PHP class: Global variable as property in class

You can use the global keyword to access a global variable inside a class.

?: operator (the 'Elvis operator') in PHP

The Elvis operator, represented by a question mark followed by a colon (?:), is a ternary operator in PHP that is used to simplify expressions that return a value based on a condition.

PDO with INSERT INTO through prepared statements

To insert data into a database using PDO and prepared statements, you can use the following steps:

Add data dynamically to an Array

To add data dynamically to an array in PHP, you can use the $array[] = $value syntax.

How to install Laravel's Artisan?

To install Laravel's Artisan, you will need to install the Laravel framework on your machine first.

PHP mySQL - Insert new record into table with auto-increment on primary key

To insert a new record into a MySQL table that has an auto-increment primary key, you can use the INSERT statement with a list of comma-separated values.

'AND' vs '&&' as operator

In PHP, both 'AND' and '&&' are logical operators that are used to test if two statements are true.

How to print all properties of an object

To print all properties of an object in PHP, you can use the get_object_vars function, which returns an associative array of an object's properties.

imagecreatefromjpeg and similar functions are not working in PHP

There could be a few reasons why the imagecreatefromjpeg function and similar functions are not working in your PHP script.

PHP float with 2 decimal places: .00

To format a float to have 2 decimal places in PHP, you can use the number_format() function.

Upgrading PHP on CentOS 6.5 (Final)

Upgrading PHP on CentOS 6.5 can be a bit tricky, since CentOS 6.5 uses an older version of PHP.

What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

Long-polling, WebSockets, Server-Sent Events (SSE), and Comet are all techniques used to enable real-time communication between a client (usually a web browser) and a server.

How to display string that contains HTML in twig template?

To display a string that contains HTML in a Twig template, you can use the raw filter.

PHP Fatal error: Call to undefined function json_decode()

This error usually occurs when you are trying to use the json_decode() function, which is used to decode a JSON string into a PHP variable, but the function is not available.

PHP Regex to get youtube video ID?

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

PHP check if url parameter exists

To check if a URL parameter exists in PHP, you can use the isset function.

How to fix "set SameSite cookie to none" warning?

To fix the "set SameSite cookie to none" warning in PHP, you will need to specify the SameSite attribute when you set the cookie.

How to get ID of the last updated row in MySQL?

To get the ID of the last inserted row, you can use the LAST_INSERT_ID() function.

New lines (\r\n) are not working in email body

In order to include new lines in the body of an email sent with PHP, you will need to use the proper new line characters.