Deprecated create_function() call [duplicate] - php

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 3 years ago.
I just updated my server to the latest version of php 7.2 and now I have some depreciation warnings. What should I do?
This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
Here is my code:
if(!array_key_exists('callable', $this->translation_plural)) {
$this->translation_plural['callable'] = create_function('$n', $this->translation_plural['function']);
}

The documentation recommends using anonymous functions. Given that $this->translation_plural['function'] looks like it is a string, you should consider a rewrite.
If you want to get rid of the warning, you can use the following:
$this->translation_plural['callable'] = function($n) { return eval($this->translation_plural['function']); };
This doesn't help your code at all, you are still using eval() which is bad practise. The documentation warns against using it.
The only difference is, create_function() used it internally, now it is very explicit.

Related

PHP Function create_function() is deprecated , the code cant work in newest PHP? [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 1 year ago.
I have a function which im not entirely sure how to convert it to get working in newest php
$eventSponsor = array_map(create_function('$o', 'return $o["id"];'), $event->sponsors);
which method should i use in newest php version ?
yeah i was searching and i found this method called anonymous function so the code be like
$awe function ($o) {
return $o["id"];
};
$eventSponsor = array_map($awe,$event->sponsors); ```

PHP 7.3 Function create_function() deprecated from 5.6 update [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 2 years ago.
I have recently upgraded a site im working on from PHP Version 5.6 to 7.3
The problem I am having is with a deprecated function in my wordpress functions.php
add_filter('max_srcset_image_width', create_function('', 'return 1;'));
How would i rewrite the above code for 7.3
I have already tried:
add_filter('max_srcset_image_width', function('', 'return 1;'));
Thanks
You can use functions directly like closures.
add_filter('max_srcset_image_width', function() { return 1; });
Update:
Since PHP 7.4 you can use shorthand arrow functions and write it shorter.
add_filter('max_srcset_image_width', fn() => 1);

I have a deprecation error occuring on PHP 7.4.3 [duplicate]

This question already has answers here:
PHP 7.4 deprecated get_magic_quotes_gpc function alternative
(2 answers)
Closed 1 year ago.
I am a beginner in php. I am running the final chapter project named Robin's Nest from the book "Learning PHP, MySQL and JavaScript with jQuery".
In the file function.php, the function sanitizeString raises the following deprecation error:
Deprecated: Function get_magic_quotes_gpc() is deprecated in C:\xampp2\htdocs\MyProjects\robinsnest\functions.php on line 42.
My version of php is 7.4.3 and I use Xampp on Windows 10 operating system. I checked the PHP manual for the function and saw that the function has been deprecated as of PHP 7.4.0.
How can I modify this function so that it doesn't use the deprecated function get_magic_quotes_gpc() and still get equivalent functionality as there seem to be no replacement function?
function sanitizeString($var)
{
global $connection;
$var = strip_tags($var);
$var = htmlentities($var);
if (get_magic_quotes_gpc())
$var = stripslashes($var);
return $connection->real_escape_string($var);
}
Just get rid of the code that uses the function. It hasn't been relevant for many versions.
function sanitizeString($var)
{
global $connection;
$var = strip_tags($var);
$var = htmlentities($var);
return $connection->real_escape_string($var);
}
Note that this sanitization is not appropriate to begin with. htmlentities() should only be used when you're displaying data on a web page, not when you're processing input. And $connection->real_escape_string() should only be used when substituting variables into a SQL string, but it's better to use prepared statements for this.

How to use array_filter with callback in php 5.2 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I am trying to use array_filter with call back in php 5.2, but I get the following error:
Parse error: syntax error, unexpected T_FUNCTION
And I did search the solution using the error in Google search and found that Php 5.2 does not support callback. The code I am working on is:
$result = array_filter($lines, function($line) {
return stripos($line,"ID:")!==false;
});
How do I change it so that It can work in php 5.2? Any help and workaround would be very much appreciated. Thanks.
Anonymous functions was introduced in PHP 5.3, so if you are using PHP 5.2 or lower you need to define the function explicitly and pass the functions name as the second argument of array_filter(), as shown below.
$result = array_filter($lines, 'filter');
function filter($line) {
return stripos($line,"ID:") !== false;
}
Consider upgrading to a newer version of PHP if you can.
PHP.net on array_filter()
PHP.net on anonymous functions
PHP.net on the Closure class

how to solve the ereg function deprecated error [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I am working with SEO PHP scripts and I am just following Google SEO scripts. When I used the search terms I got an error like the following:
Deprecated: Function eregi() is deprecated in E:\wamp\www\subgoogle\nusoap.php on line 3876
Deprecated: Function ereg() is deprecated in E:\wamp\www\subgoogle\nusoap.php on line 3896
Deprecated: Function ereg() is deprecated in E:\wamp\www\subgoogle\nusoap.php on line 1451
How should I remove that error function? Is there any need to use a library?
eregi() function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.
you can use preg_match().
http://php.net/manual/en/function.eregi.php
Note:
As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE.
You need to convert every use of ereg* to an equivalent function of the preg_* family. Or, as #Srisa rightly points out, look for an updated version of the library/script in question.
error_reporting( 0 ) ;
That's how you can eliminate the symptoms, but to cure the disease you just shouldn't use POSIX regular expressions, change them to PCRE
you may want to check this brunch http://sourceforge.net/projects/nusoapforphp53/
it works for me
Change ereg() to mb_ereg.hope which fixes your error. Good luck!

Categories