Tag: PHP

PHP (recursive acronym: Hypertext Preprocessor) is a widely-used open source scripting language that is especially suited for web development. It allows developers to create dynamic web pages, interact with databases, and perform various tasks to build robust and interactive websites.

  • WordPress: how to remove the ‘link rel=shortlink’ tag from your site

    WordPress: how to remove the ‘link rel=shortlink’ tag from your site

    Written by

    in ,

    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…

    Read more

  • How to delete a single element from an array in PHP

    How to delete a single element from an array in PHP

    Written by

    in

    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: The output of print_r() is: array_splice() This function can be used to remove a portion of an array and replace…

    Read more

  • PHP fatal error: Uncaught TypeError: ftp_nlist(): Argument #1 ($ftp) must be of type FTP\Connection, null given

    PHP fatal error: Uncaught TypeError: ftp_nlist(): Argument #1 ($ftp) must be of type FTP\Connection, null given

    Written by

    in ,

    After moving a WordPress installation to another server, the following error showed up: 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…

    Read more

  • PHP: when to use ‘self’ and when to use ‘$this’?

    PHP: when to use ‘self’ and when to use ‘$this’?

    Written by

    in

    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…

    Read more

  • PHP: How to check if a string contains a specific word?

    PHP: How to check if a string contains a specific word?

    Written by

    in

    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…

    Read more

  • Pecl: fixing “fatal error: ‘pcre2.h’ file not found”

    Pecl: fixing “fatal error: ‘pcre2.h’ file not found”

    Written by

    in

    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: The output was: To fix the issue, make sure you have pcre2…

    Read more

  • PHP: Function utf8_decode() and utf8_encode() have been deprecated

    PHP: Function utf8_decode() and utf8_encode() have been deprecated

    Written by

    in

    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…

    Read more

  • PHP: rounding a number and keeping the zeros after comma

    PHP: rounding a number and keeping the zeros after comma

    Written by

    in

    In PHP you can use the round() method to round a double. This methods accepts a precision value as second parameter. Some examples: When using round() on a value like 3.0000 the conversion to a string will result in just “3”: This is not wrong, but when you want to have a constant precision for…

    Read more

  • PHP: get version details from composer.json

    PHP: get version details from composer.json

    Written by

    in

    Let’s say we have a composer.json file as follows: When using this file to manage the packages of an app, it might be necessary to check for the version of your app. This is possible by using the Composer class \Composer\InstalledVersions. The class provides different methods to access the details of the projects composer.json file.…

    Read more

  • PHP: realpath() for non-existing path

    Written by

    in

    The php method realpath() can transform the string format of a path into a real path. Means, a path string like: will become: But this only works, if the path really exists. For non-existing paths, this function cannot be used. To get the same functionality, the following function can be used:

    Read more

  • How to ignore PHP_CodeSniffer warning: Line exceeds 120 characters

    How to ignore PHP_CodeSniffer warning: Line exceeds 120 characters

    Written by

    in

    When using CodeSniffer to check your code, a lot of warnings might appear when the lines are too long: There are different possibilities to ignore warnings or errors of code lines. A full list of CodeSniffer features to handle the warnings and errors is documented in the CodeSniffer Wiki. Ignore a single code line To…

    Read more

  • PNG – deactivate interlace to avoid ‘libpng warning: Interlace handling should be turned on when using png_read_image’

    Written by

    in

    I stumbled appon a warning message that was thrown by PHP when handling images with GD lib (e.g. imagecreatefrompng()). The message shown was: This message even exists, when deactivating ‘interlace’ with the help of: The point is that this message is not caused by any wrong php code, it is caused by the processed images…

    Read more

  • PHP ColorUtils

    PHP ColorUtils

    Written by

    in

    A neat collection of useful methods to convert colors between different color spaces is available on GitHub (MIT License). It can convert any values between RGB, HSL, CMYK, YUV and HEX colors. I created a ready-to-use php class out of this method collection: https://github.com/mixable/color-utils Usage The methods itself are mostly self-explaining. To use the class,…

    Read more

  • Fatal error: Call to undefined function bindtextdomain()

    Written by

    in

    This error might be caused by a missing GetText extension. If this is the case, you have to enable the extension in your php.ini: If the extension is not installed yet, this is how to install gettext on macOS (for PHP7.0): Afterwards, do not forget to restart Apache:

    Read more

  • PHP: Wie bekommt man Gettext zum Laufen?

    Written by

    in

    Zunächst erstellt man sich ein Verzeichnis, z.B. locales/, in dem die Übersetzungsdateien abgelegt werden. Die Verzeichnisstruktur sollte Unterverzeichnisse für jede Sprache, beispielsweise en/ für Englisch, de/ für Deutsch, usw. enthalten und wie folgt aufgebaut sein: locales/ de/ LC_MESSAGES/ default.po default.mo en/ LC_MESSAGES/ … In diese Verzeichnisse kommen dann die Übersetzungsdateien (*.mo, *.po). Wie diese aufgebaut…

    Read more

  • MacPorts: mehrere PHP-Versionen gleichzeitig

    Written by

    in

    MacPorts unterstützt jegliche PHP-Versionen. Diese können auch ohne Probleme gleichzeitig installiert werden. Die Pakete werden in MacPorts unter php56, php70, php71, … usw. gelistet. Die notwendigen Befehle, um eine einzelne Version zu installieren: MacPorts aktualisieren: PHP Version (beispielsweise PHP 7.0) und ggf. erforderliche Module installieren: Nun zum Aktivieren der gewünschten PHP-Version: hier muss beachtet werden,…

    Read more

  • Composer global verwenden

    Written by

    in

    In der Dokumentation zu Composer wird erläutert, wie man global auf composer.phar zugreifen kann, ohne permanent php composer.phar ins Terminal eingeben zu müssen: […] You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your PATH, you can access it globally. On unix systems you…

    Read more

  • Imagick und “libgomp: Thread creation failed: Resource temporarily unavailable”

    Written by

    in

    Bei der Bildbearbeitung mit Imagick (PHP) kann es mitunter zu einem Timeout bzw. Internal Server Error kommen und die Grafik wird nicht erstellt. In meinem Fall immer dann, wenn man versucht einen Text in der Grafik mittels Imagick::annotateImage() darzustellen. Schaut man sich die error.log des Webservers an, dann stolpert man über den Fehler “libgomp: Thread…

    Read more

  • Apache2, PHP und MySQL über MacPorts installieren

    Es gibt hier im Blog bereits eine Vielzahl an Posts zu diesem Thema. Warum? Mit jedem Update von Mac OS X scheint sich wieder etwas zu ändern, sodass die Installation von Apache / PHP / MySQL mittels MacPorts nicht mehr funktionieren möchte. Was bei den einzelnen OS X Versionen zu beachten ist, ist in den…

    Read more

  • Firefox – Content Encoding Error / Google Chrome – ERR_CONTENT_DECODING_FAILED

    Written by

    in

    Beim Öffnen einer Webseite, erhält man folgenden Fehler in Google Chrome: Error 330 (net::ERR_CONTENT_DECODING_FAILED): Unknown error Nutzt man FireFox dann sieht dieser Fehler so aus: Content Encoding Error The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression. Woran liegt das?

    Read more