I'm trying to migrate a script in php 5.2 to 5.5 that uses set_magic_quotes_runtime() to disable them. I found a hint that suggested I could replace it with:
ini_set ("magic_quotes_runtime", 0);
Is this correct?
Simply delete the line.
Magic quotes have been dead for some time, and have been removed in PHP 5.4 and later. There is no reason to force them to off when they don't exist.
If you are trying to turn them off, there's nothing to be done - the feature has been removed from PHP entirely since PHP 5.4. There's no need for the line you cited at all in PHP 5.5.
If you are trying to turn them on, you would need to recreate the feature in userland using something like str_replace(); however, I would urge you not to do it.
Related
My website has been running for a long time.
Currently upgraded to php 5.6 and maria db 10.1.44.
The utf8_encode function was used in a lot of php code.
But now I have to remove it so the characters are output correctly.
However, I can't edit a lot of php code ... Is there a way to invalidate the utf8_encode function?
Or is it possible to override the php core function?
Yes you could do so by making some changes in the php.ini file as
Disable PHP Functions
You could override utf8_encode() with function_override() but I can not recommend it.
Instead, you better fix the existing code yourself. When you use an IDE like Eclipse PDT, you can do a global search in all files and folders, and remove the calls to utf8_encode() rather quickly.
While PHP 5.6's default encoding has been changed to UTF-8, that version is already end-of-life: https://www.php.net/supported-versions.php. After moving up to 5.6, consider migrating your code to PHP 7.2 at least.
I am trying to run Imagick within Laravel 5 and I keep getting this error every time I try and run my code. Imagick::paintTransparentImage method is deprecated and it's use should be avoided. Before when I was running the code without Laravel this was a warning but the function would still run. Is there any way to suppress or turn off this warning? All other Imagick functions work great, and this one does if run outside the context of Laravel.
as you mentioned, Imagick::paintTransparentImage method is deprecated.
Use Imagick::transparentPaintImage instead.
http://php.net/manual/en/imagick.transparentpaintimage.php
I ended up turning turning down the php error reporting strictness and the code runs fine. If anyone else gets this message do not freak out, it is depreciated but they have NOT provided an up to date alternative, opaquePaintImage is NOT the same thing.
Existing product developed in core PHP is built in PHP 5.2 and now we want to migrate the product to PHP 5.4.
CodeSniffer can detect compatibility for version and generate report with warnings and errors.
Current approach could be:
Scan whole product and get report from CodeSniffer
Prepare a plan to fix each type of warning or error, like replace ereg_replace with preg_replace function and in first argument add delimiters
Example:
Original: $new = ereg_replace(“oldstring“, “newstring“, $old);
Replace: $new = preg_replace(“/oldstring/“, “newstring“, $old);
Make a PHP script to implement the plan
Run the script, test with CodeSniffer again and run a test on whole product again
Is there any better tool or approach for migration PHP 5.2 to PHP 5.4?
I have PHPUnit tests for my project. The tests also record all warnings and notices. With PHP5.2 I made sure that had no warnings or notices. Then I upgraded to PHP 5.4 and got lots of warnings and notices, plus many tests failed. Then it was just a matter of fixing all of them.
The most common issue that I had: assigning properties of uninitialized objects.
I would collect info about these Warnings or Errors and parse them for a filename and string number on which "failure" occurs and in manual mode (simplest approach) correct all these issues.
Even if project is too big, I think such cases wouldn't be too much, otherwise better think about refactoring than correcting compatibility issues.
I am running PHP version 5.1.6 currently, but I would have an application which makes pretty heavy use of the json_encode and json_decode functions, as such, I would like to add these functions to my server's install of PHP (as these functions only ship with PHP versions PHP 5.2 +)
What is the best way of doing this. I'm not too down with the whole Terminal approach so if there was another way that would be great
Many thanks in advance
If you're unable to upgrade, use PEAR's Services_JSON. It works as of PHP 4.3 so you should be fine.
Maybe you should put somewhere in the root of your application a call to a checker function that does function_exists() for a couple of your needed function and if not stops the execution to prevent unwanted results.
So I was reading about PHP namespaces, and I realized that in versions earlier than 5.3, if you write
namespace MyNamespace
you get a parse error.
Is there any way to avoid this i.e. make namespaces backwards-compatible, so the code doesn't simply crash?
Short Answer: No.
Longer Answer: (added to capture useful information from other deleted answers). The new Syntax will cause parse errors in PHP, so you can't use a customer error handler to catch errors generated in versions < 5.3. In theory you could write a pre-processor the scans and/or does a lex/parse on the source and then write something back out that would be PHP 5.2 compatible, but that creates more problems than it solves.
Perhaps you could query the version of PHP being used and call eval if it's high enough. I don't know if that will work though.
Actually, I think it's possible, but I don't believe it's worth it. The idea would be to create a custom default stream wrapper, which will parse PHP files according to the new grammar and make the appropriate changes to the syntax so that it will be valid PHP < 5.3.
The wrapper would have to replace class names such as Foo\Bar\Baz with Foo_Bar_Baz. Currently I'm not sure if there's something that would render this impossible.
Anyway, I don't believe it's worth the effort. Upgrade to PHP 5.3.
Oh, that means that the wrapper code should be compatible with PHP < 5.3.
I know this is a very old question, but I needed to make a couple of instructions with namespaces backward compatible with a very old PHP installation (5.2).
What I finally did to avoid parse errors was:
if(version_compare(PHP_VERSION, '5.3.0') >= 0) {
include("file_with_namespaces_code.php");
}
else{
echo("put php 5.2 code here");
}