How-to articles, tricks, and solutions about PHP

Codeigniter Rename file on upload

In CodeIgniter, you can rename a file on upload by using the do_upload() method of the Upload class.

PHP/regex: How to get the string value of HTML tag?

In PHP, you can use the preg_match() function to extract the string value of an HTML tag.

Creating new Date Time from string

In PHP, you can use the DateTime class to create a new date and time object from a string.

create folder in laravel

In Laravel, you can create a new folder using the File facade's makeDirectory method.

MySQL Check if username and password matches in Database

To check if a given username and password match those stored in a MySQL database, you can use a SQL SELECT statement with a WHERE clause.

relative path in require_once doesn't work

The require_once function in PHP is used to include a specific PHP file in another file, but it will only include the file once to prevent multiple declarations.

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,

This error message is indicating that there is an issue with an entity reference in the HTML code being parsed by the DOMDocument class.

Composer: remove a package, clean up dependencies, don't update other packages

To remove a package with Composer, you can use the command composer remove [package-name].

proper way to logout from a session in PHP

To properly log out of a session in PHP, you can use the session_destroy() function.

How to get id from URL in codeigniter?

In CodeIgniter, you can get the value of the id from the URL by using the function $this->uri->segment(n), where n is the position of the segment you want to retrieve.

Given a Unix timestamp, how to get beginning and end of that day?

You can use the strtotime() function in PHP to convert a Unix timestamp to the beginning and end of that day.

Split text string into $first and $last name in php

In PHP, you can use the explode() function to split a string into an array, where each element of the array is a substring that was separated by a specified delimiter.

PHP: HTTP or HTTPS?

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

Setting public class variables

In PHP, you can set the value of a public class variable by using the assignment operator (=) after the variable name.

HTTP authentication logout via PHP

To log out a user from HTTP authentication using PHP, you can use the header function to send a "WWW-Authenticate" header with a value of "Basic realm=realmName" and a 401 status code.

Warning: mysqli_connect(): (HY000/2002): No such file or directory

This error message is indicating that there is a problem connecting to the MySQL server.

HTTP requests with file_get_contents, getting the response code

To make an HTTP request using the file_get_contents function in PHP and retrieve the response code, you can use the following code snippet:

MySQL Great Circle Distance (Haversine formula)

The Haversine formula is a mathematical formula that can be used to calculate the great-circle distance between two points on a sphere, such as the Earth.

How to use the php that brew installed?

To use the PHP that Homebrew installed, you will first need to make sure that it is in your system's PATH.

How to check if a string starts with "_" in PHP?

In PHP, you can use the substr() function to check if a string starts with a specific character or substring.

Laravel 5 - Interface is not instantiable

This error message in Laravel 5 typically means that you are trying to instantiate an interface, but interfaces cannot be instantiated because they are only a contract for the methods that a class should implement.

Invoking a PHP script from a MySQL trigger

It is possible to invoke a PHP script from a MySQL trigger by using the system() function in the trigger's body.

php artisan migrate - SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'localhost'

This error message is indicating that the PHP script, "artisan," is attempting to run a database migration command, but the database server is denying access to the user "laravel" on the "localhost" location.

Confirm button before running deleting routine from website

Yes, you can add a confirm button before running a delete routine on a website using PHP.

search and replace value in PHP array

In PHP, you can use the array_map() function to search and replace values in an array.