By default, WordPress adds <link rel="shortlink"> meta tag to the <head> of a website and uses the short url like https://mixable.blog/?p=12345 for this. When you already use nice slugs as permalink structure, such a tag is not necessary, because you already have unique urls.
To remove the shortlink tag, you can use an additional plugin or simply add some code to your themes functions.php:
There are multiple ways to remove an element from an array in PHP. The simplest one is the method unset().
unset()
The method unset() can be used to remove a single element of the array:
PHP
$myArray = ['apple', 'banana', 'cherry', 'date'];unset($myArray[1]); // Remove the element at index 1 (banana)$myArray = array_values($myArray); // Re-index the array if you want to remove the gapprint_r($myArray);
This function can be used to remove a portion of an array and replace it with something else. If you want to remove a single element, you can specify a length of 1:
PHP
$myArray = ['apple', 'banana', 'cherry', 'date'];array_splice($myArray, 1, 1); // Remove 1 element starting from index 1print_r($myArray);
array_diff()
You can use this function to create a new array with all the elements of the first array that are not in the other arrays:
This error happened after moving a WordPress installation to another server. The new server had a different linux distribution and a newer PHP version. In my case, the environment changed from PHP 7.4 to PHP 8.2.
I already added some missing PHP extensions and updated the configuration to match the old one, but the error still exists.
At the end, this could be solved by adding the following code in wp-config.php file:
In PHP, the keyword $this is used to refer to the current instance of the class, while the keyword self is used to refer to the class itself.
You should use $this when referring to instance-level properties or methods within the same class, such as when accessing a property of the current object or calling a method of the current object.
For example:
classMyClass {private$name;publicfunction__construct($name) {$this->name = $name; }publicfunctionsayHello() {echo"Hello, my name is ".$this->name; }}$obj = newMyClass("John");$obj->sayHello(); // output: "Hello, my name is John"
On the other hand, you should use self when referring to static properties or methods within the same class, such as when accessing a static property or calling a static method of the class.
You can check if a string contains a specific word in PHP by using the strpos() function, the preg_match() function or the str_contains() function.
Using strpos()
The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found, it returns false. You can use this function to check if a string contains a specific word as follows:
$string = "This is a sample string.";$word = "sample";if (strpos($string, $word) !== false) {echo"The string contains the word.";} else {echo"The string does not contain the word.";}
In this example, we first define a string $string and a word $word. Then, we use the strpos() function to check if the $string contains the $word. If the $word is found in the $string, the function will return a position value that is not false, and the output will be “The string contains the word.” Otherwise, the function will return false, and the output will be “The string does not contain the word.”
Using preg_match()
The preg_match() function searches a string for a pattern and returns true if the pattern is found, and false otherwise. You can use this function to check if a string contains a specific word as follows:
$string = "This is a sample string.";$word = "/sample/";if (preg_match($word, $string)) {echo"The string contains the word.";} else {echo"The string does not contain the word.";}
Using str_contains()
The str_contains() method is available in PHP 8 and higher versions. You can use this method to check if a string contains a specific word as follows:
$string = "This is a sample string.";$word = "sample";if (str_contains($string, $word)) {echo"The string contains the word.";} else {echo"The string does not contain the word.";}
Performance Comparison
When comparing the performance of those three methods, we get the following result:
strpos()
1.37e-7 seconds = 0.137 micro seconds
preg_match()
1.54e-7 seconds = 0.154 micro seconds
str_contains()
1.28e-7 seconds = 0.128 micro seconds
So the new method str_contains() (for PHP 8.0 or higher) is the fastest one.
For the results, the mean execution time in a loop with 1,000,000 cycles was calculated for each method.
When using pecl to install a PHP extension, I always got a “fatal error: ‘pcre2.h’ file not found” after PHP has been updated. The update was done using brew upgrade php. In my case, this happens when I try to install pcov using:
pecl install pcov
The output was:
In file included from /private/tmp/pear/temp/pcov/pcov.c:26:/opt/homebrew/Cellar/php/8.2.2/include/php/ext/pcre/php_pcre.h:23:10: fatal error: 'pcre2.h' file not found#include "pcre2.h" ^~~~~~~~~
To fix the issue, make sure you have pcre2 installed:
Make sure, to adjust the versions of pcre2 and php (or any other package where you got the error). In my case it’s PHP version 8.2.2 (see the error message) and pcre2 version 10.42.
After the symlink was created, the installation of pcov finished without errors:
Build process completed successfullyInstalling '/opt/homebrew/Cellar/php/8.2.2/pecl/20220829/pcov.so'install ok: channel://pecl.php.net/pcov-1.0.11Extension pcov enabled in php.ini
The utf8_encode() and utf8_decode() functions in PHP are used for encoding and decoding strings between ISO-8859-1 (Latin-1) encoding and UTF-8 encoding.
While PHP’s standard library does include utf8_encode and utf8_decode functions, they are limited to converting between ISO-8859-1 (Latin-1) and UTF-8 encodings. It is important to note that these functions cannot be relied upon to detect and convert other character encodings, such as Windows-1252, UTF-16, and UTF-32, to UTF-8. Attempting to use these functions with arbitrary text can introduce bugs that may not produce any warnings or errors, but can result in unexpected and undesired outcomes.
Examples of common bugs that can occur include:
The Euro sign (€, character sequence\xE2\x82\xAC), when passed to utf8_encode function as utf8_encode("€") results in a a garbled (also called as “Mojibake”) text output of â¬.
The German Eszett character (ß, character sequence\xDF), when passed through utf8_encode("ß") results in Ã.
The utf8_encode and utf8_decode functions have been deprecated in PHP 8.2 due to their misleading function names, lack of error messages and warnings, and their inability to support character encodings other than ISO-8859-1.
As a result, using these functions in PHP 8.2 will emit a deprecation notice. It is recommended to use alternative functions or libraries that provide better support for handling different character encodings. These functions will be removed entirely in PHP 9.0, so it is important to migrate to alternative solutions as soon as possible to avoid compatibility issues in future versions of PHP.
utf8_encode('foo');// Function utf8_encode() is deprecated in ... on line ...
uft8_decode('foo');// Function uft8_decode() is deprecated in ... on line ...
Replacement for the deprecated functions
Instead, the PHP documentation recommends using the multibyte string functions that are part of the mbstring extension for handling multibyte encodings, including UTF-8. For example, the mb_convert_encoding() function can be used to convert strings between different character encodings, including to and from UTF-8.
Replacement for utf8_encode()
Here is an example of how to use mb_convert_encoding() to encode a string to UTF-8:
$string = "Some string with non-ASCII characters: é, ö, ü";$utf8_string = mb_convert_encoding($string, 'UTF-8');
Replacement for utf8_decode()
And here is an example of how to use mb_convert_encoding() to decode an UTF-8 string: