Downgrade Ubuntu PHP Version from PHP 8 to PHP 7.4

Recently my Ubuntu server updated itself, and I suddenly had version PHP 8.0 on my terminal.

I use PHP for composer installations, and some packages in my composer.json required PHP 7.4 and wouldn’t install with PHP 8.

So I had to downgrade my PHP Version from PHP 8.0 to PHP 7.4

This is done like that:

sudo a2dismod php8.0
sudo a2enmod php7.4
sudo service apache2 restart

sudo update-alternatives –set php /usr/bin/php7.4
sudo update-alternatives –set phar /usr/bin/phar7.4
sudo update-alternatives –set phar.phar /usr/bin/phar.phar7.4
sudo update-alternatives –set phpize /usr/bin/phpize7.4
sudo update-alternatives –set php-config /usr/bin/php-config7.4

Not every command is neccessary, but it won’t hurt to list and execute them all. You can just copy&paste these 8 lines into your bash.

And now we are running PHP 7.4 again and can install our composer dependencies:

$ php -v
PHP 7.4.16 (cli) (built: Mar 5 2021 07:54:02) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies

If you get messages about missing raphf or propro modules, do:

sudo pecl install raphf
sudo pecl install propro

After this, you need to add these modules to your php.ini config.
1. find your php.ini

$ php -i | grep php.ini
Configuration File (php.ini) Path => /etc/php/7.4/cli
Loaded Configuration File => /etc/php/7.4/cli/php.ini

Add these 2 lines to your php.ini, preferably to the block with the extensions:

… snip …
;extension=pgsql
;extension=shmop
extension=raphf.so
extension=propro.so

; The MIBS data available in the PHP distribution must be installed.
… snip …

Write PHP debugging output to a file instead of echoing it

PHP debugging made easy

At some point in our life as PHP developer, we come to the point where we are debugging PHP by checking the values of our variables, arrays and objects.
We all know: Its easy to debug in Javascript. You can do something like:
console.log("variable is:"+myVar);

This is a problem for PHP debug.
You can echo() in your code, but thats not always possible.
Output thats printed in a controller action often disappeares from the screen when the site is rendered.
Sometimes the very own debug function of your framework overwrites everything we tried to display.
Sometimes your layout will break, or when doing AJAX calls you might not even see anything.
So how can you see your precious variables, objects and SQL commands at runtime without breaking the app?

A way to debug some values is writing them to a file.

Example:

$myVariable = "Hey, Mr. Coder!";
$myArray = ['color' => 'green','feet' => 'big','dog' => 'Retriever'];

$debugfile = fopen("debug.txt", "w");

fwrite($debugfile, $myVariable."\n");
fwrite($debugfile, print_r($myArray,true));

fclose($debugfile);

You should notice the “w” in the fopen command. This is called the “mode”. Mode “w” means your file will always be cleared when writing to it.
If you dont want that, but rather keep all records in the file, you should use mode “a”:

$debugfile = fopen("debug.txt", "a");

Do you see the “\n” in the first fwrite command? This writes a newline to the file. Otherwise the following array would be written to the same line as the variable and the whole debug output would be hardly readable.

For a list of all modes and more examples you should head over to http://php.net/manual/de/function.fopen.php.

Show static google map image and link to map with marker

With this snippet you can show a static google map image for a specific location, which is linked to google maps and includes showing a marker in all views.
Don’t forget to change YOUR_GOOGLE_STATIC_MAPS_API_KEY to your api key, which you can get at https://console.developers.google.com by creating a server key and aactivate google maps for this account.


$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$address = urlencode("Kupferbergterrasse 10, 55116 Mainz");
$request = file_get_contents("https://maps.google.com/maps/api/geocode/json?address=" . $address . "&key=YOUR_GOOGLE_STATIC_MAPS_API_KEY&sensor=false", false, stream_context_create($arrContextOptions));
$json = json_decode($request, true);
$lat = $json['results'][0]['geometry']['location']['lat'];
$lng = $json['results'][0]['geometry']['location']['lng'];
$marker = "color:blue|label:ZIEL|".$lat.','.$lng;
echo '<a href="https://www.google.com/maps/place/'.$address.'"><img src="https://maps.googleapis.com/maps/api/staticmap?center='.$lat.','.$lng.'&zoom=14&size=600x300&markers='.$marker.'&key=YOUR_GOOGLE_STATIC_MAPS_API_KEY"></a>';

demo:

staticmap

Exercise for Frontend-Developer to test PHP-, CSS3- and HTML-skills.

Task: Create a slider from the entries of an an XML-File.

Design draft is here: http://pascha.org/aufgabe/slider.png

XML-File is here: http://pascha.org/aufgabe/comments.xml

Icons & Elements: http://pascha.org/aufgabe/pascha.org-aufgabe-icons.zip

Guidelines:
– You can use jQuery
– The entries in the XML are pointing to facebook or google, so the respective logo should be shown in the slider.
– The Slider should jump from the last to the first entry.
The slider should be draggable and use touchevents.
– The “Read more”-Button (“weiterlesen”) should be linked to the respective page.

Multiple step animation of an element with CSS only

If you want to move elements around the screen in multiple steps, you can use CSS animations for that.

Demo:

HOVER ME

Continue reading

Move/animate div or element to screen corner on click or hover with CSS only

If you want to move an element to a screen corner with CSS only, theres a simple Solution with CSS3.

Demo:

Continue reading

A responsive 2 row fluid layout with mobile view

A 2 (or multiple) row responsive fluid layout, one row has fixed width, all rows have same height, a margin is between rows.
It collapses in mobile resolutions.

The code is minimal, no freaky negative margins or paddings. It’s very easy to use and understand.

DEMO:

Fluid width row

Your bones don’t break, mine do. That’s clear. Your cells react to bacteria and viruses differently than mine. You don’t get sick, I do. That’s also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke. We get some in our lungs, we drown. However unreal it may seem, we are connected, you and I. We’re on the same curve, just on opposite ends.
(Text by http://slipsum.com)

Fixed width row

Change browser width to see it respond.
Continue reading