Differences between php v7 and v8

Hi

Im not a php developer per se, but I know enough to get by.

Ive been tasked with upgrading a basic php v7 website to php v8. I know the shorthand tags wont work, are there any other stumbling blocks that I may come across? I know this is a very very nebulous question. Perhaps theres a cheat sheet of things that have changed between php v7 to v8?

Thanks

You’ll be wanting the migration guides and from 8.0 to 8.1.

2 Likes

Depends on the functions that website uses really. If that website is well up to date with standards then upgrading from PHP 7 to PHP 8 will be a breeze.

2 Likes

Become familiar with php.ini which is updated with every PHP Version and also note the location varies depending on the operating system. Please note that php.ini contents are compiled and any syntax errors results in the default php.ini file being used without any notification!

When developing locally find the relevant php.ini location and ensure the following are set by removing preceding ;

  1. error_reporting = E_ALL # or -1 which is maximum error reporting
  2. display_errors = On # show on screen
  3. display_startup_errors = On # prevent blank screen on syntax errors
  4. search for <span and </span and modify to your requirements.

With item #4 I have the following modifications:
a. copy and paste the lines
b. remove leading ;
c. change span to pre
d. add style=“width:88%; margin: 1rem auto; background: AQUA, color: #000:, etc”

The above modifications make errors and warnings far more readable otherwise a vast amount of errors and warnings are shown without linefeeds, etc. Don’t forget to compile the modifications by calling the following Command Line:

systemctl reload apache2

Edit:

Add the following command to the start of every PHP file because unlike error_reporting, display_errors, etc the declaration only applies to that file and is not inherited.

<?php declare(strict_types=1);

Test the above by setting the following:

ini_set(“display_errors”, 1);

Php has an excellent online manual, learn how to search and use the manual :slight_smile:

1 Like

Thanks guys. This is all very useful! Ill be doing a lot of bedtime reading!

Regards the short tags no longer being supported in v8, my colleague mentioned to me yesterday that this had actually been rescinded, and will continue to be supported until v9. Is that the case?

1 Like

Hmm. My code has failed at the first hurdle! :laughing:

This is because get_magic_quotes_gpc() is redundant now.

How can I make this code v8 friendly?

if (get_magic_quotes_gpc()) {
   $value = stripslashes($value);
}

magic_quotes was removed a very long time ago. After that point there was no need to test for the gpc setting being on and apply stripslashes() to any external value, because php was no longer altering the value. The get_magic_quotes_gpc() function always returned false after that point, so the stripslashes() code was always skipped. Now that get_magic_quotes_gpc() function has finally been removed, you just use the $value as is.

1 Like

Yeah, magic_quotes was deprecated in 5.3 (2009) and removed in 5.4 (2012).

LOL :sweat_smile:

Hi. Im starting to get to grips with some of the common differences now.

Quick question: Im getting “PHP Fatal error: Uncaught ValueError: setcookie()”

My code:

setcookie (session_id(), "", time() - 3600);
session_start();
unset($_SESSION['logged_in']);
session_destroy();
header ("Location: index.php");
exit;

Its obviously the second parameter of the setcookie() function need to be specified. I presume I can just set the value as “1” or something?

That’s an incorrect assumption. Read the setcookie() documentation. The default value for the 2nd parameter is an empty string, if you don’t supply that parameter.

The problem is the 1st parameter. session_id(), when called with no input parameter, returns the current session id, if there is one (requires session_start() to be called first), or returns an empty string. If sessiont_id() is called with an input parameter, before session_start() is called, that parameter value is used as the id when the session is started. This whole line of code never did anything useful, but didn’t fail with a fatal error before. What has probably changed is the error level/handling for the 1st, required, setcookie() parameter.

The session can hold information other then the login value, so destroying the whole session can cause data in a more complicated application to disappear. The only piece of data that the login/logout code is responsible for is $_SESSION[‘logged_in’], and that’s the only thing this code should be affecting.

Excellent. Thanks - I fixed that!

Hi guys,
Its going well so far. Ive currently integrated the latest TCPDF and PHPMailer libraries.

However, Im getting:
Fatal error: Uncaught Error: Call to a member function Close() on null in…

On the line

$conn->Close();

How can I fix this?
Thanks

This is telling you the value of $conn is null.
I’m guessing $conn is supposed to be a database connection, if it is there is likely to be bigger problems happening before you close it.
So it will be a case of checking your connection to find at which point it is failing.

Since php destroys all variables/resources when your script ends, there’s generally no good reason to try to close a database connection in your code. Just let php close it for you.

1 Like

Hmm, Im getting a load of errors,

Warning: Attempt to read property “FldVar” on null in… etc

I think the cause is the code trying to connect to the db. (which would also explain the $conn->Close(); issue above).

The code is:

// Connect to database
function &ew_Connect() {
	$object = new mysqlt_driver_ADOConnection();
	if (defined("EW_DEBUG_ENABLED"))
		$object->debug = TRUE;
	$object->port = EW_CONN_PORT;
	$object->raiseErrorFn = 'ew_ErrorFn';
	$object->Connect(EW_CONN_HOST, EW_CONN_USER, EW_CONN_PASS, EW_CONN_DB);
	if (EW_MYSQL_CHARSET <> "") $object->Execute("SET NAMES '" . EW_MYSQL_CHARSET . "'");
	$object->raiseErrorFn = '';
	return $object;
}

How can this be updated so php v8 handles it?

Thanks

I’ve never prefixed a function name with & and must try when on the desktop.

I would prefer using curly braces with the if statements or at least add a blank line to make the function more readable… which also makes modifications less likely to have errors.

Gone are the days of saving bytes, except in special cases. Making script readable at a glance certainly reduces time spent debugging.

Ive amended the code as you suggest, but still getting the warning/errors:

function ew_Connect() {
	$object = new mysqlt_driver_ADOConnection();
	if (defined("EW_DEBUG_ENABLED")){
		$object->debug = TRUE;
	}
	$object->port = EW_CONN_PORT;
	$object->raiseErrorFn = 'ew_ErrorFn';
	$object->Connect(EW_CONN_HOST, EW_CONN_USER, EW_CONN_PASS, EW_CONN_DB);
	if (EW_MYSQL_CHARSET <> "") $object->Execute("SET NAMES '" . EW_MYSQL_CHARSET . "'");
	$object->raiseErrorFn = '';
	return $object;
}

How far through your ew_Connect() function does it get before failing, when you debug it?

I tried the function with MySqli and PDO. The result was an object so it looks as though that function is working satisfactorily.

$obj = new &ew_Connect()  ;
echo ‘<pre>’;
print_r($obj);
die;

Does the error/warnings return any line numbers?

What operating system are you running?