Upgrade to PHP7 - Split Function

Hello everyone, I am Rick and I am glad to have found such a nice and interesting forum.
I am doing some maintenance to a web server, updating XAMPP to the latest version (32–>64bit, php 5.6–> 7.4, latest MariaDB).
This server houses a web application that was coded by an ex colleague. It was written using php5 and (:rage:) some functions which were at that time already deprecated and now gone in php7.
The function no more present is split(), and it is used within a custom funciton to get unix time using the microtime() function.

The following getmicrotime() function is present on many php files, therefore it breaks the web app as it is not compatible with php7.

<?php
function getmicrotime() { 
    $temparray = split(" ",microtime()); 
    $returntime = $temparray[0] + $temparray[1]; 
    return $returntime; 
}
$starttime = getmicrotime(); 

How can I recode this funciton to maintain it while abiding to php7? How can I use the explode() or preg_split() associated functions to recode it?
Thank you very much in advance!

split can be replaced with explode or preg_split, depending on usage. Since you’re not using regular expressions here, explode would be simplest.

You could also simplify the function considerably, as microtime can accept a flag to return the format you’re after.

Replace the function with the following:

function getmicrotime() {
    return microtime(true);
}

and you’re away.

1 Like

@Skawid thank you very much for your answer!
In the mean time I tried substituting the split function with explode, as I saw some examples of explode using the same expression.
It worked! I will also try your suggestion, as it is way more elegant and simple.
Thank you again very much!

Sponsor our Newsletter | Privacy Policy | Terms of Service