PHP 7.2 strange in_array behaviour - php

I upgraded from php 5 to 7.2, and there is no problem at local (some changes needed, but it solved).
One of my function not working in production server.
The code looks like this:
$someBool = in_array($some, array("asd", "fgh", "etc"));
If I make a var_dump on this, the result is UNKNOWN:0, but if I make a vardump exactly below this (there is no any if station before dump), the function works correctly, end the dump result will be bool (true) or bool (false).
(I don't overwrite this variable)
Again if I comment the var_dump line, the function works incorrectly.
There is anybody who has idea what can cause this behaviour?
Thanks for your help.
In the original function:
$needCmdText = in_array($fieldName,array(
'cmdpreid','cmdpostid','cmdskipid',
'cmdfixid','cmdexpid','cmdsysid'
));
Where the $fieldName always 'cmdexpid' from the client. (at my test cases)
And a funny news, a simple echo solve the problem too, but it should be under this.

I changed the php 7.2 to 7.1, which solved the problem without any code changing. (No need to dump, to solve).
I think there is a strange bug in php 7.2, which doesn't occure in windows environment.

Related

Array to string conversion exception with PHP 7

I was working with old legacy code and checked if it's running with PHP7. I got an exception (Array to string conversion) with following code:
json_decode($json_string)
->$array['id']
->foo
What helped was to use the curly brackets:
json_decode($json_string)
->{$array['id']}
->foo
My question is, was that already wrong PHP5 and if not, which change is the reason for that? Could not find anything because did not know for what to search... One idea I had is that the new introduced AST produces this behavior?
I think you are right. From what I know in PHP5 that works without any exception, but PHP 7 wants to be just a little bit more strict, because cases like this can cause a lot big problems after when not used correctly.

Having array identifiers on the end of a function in PHP?

I am having some some problems here. I am using XAMPP on a Windows box to test. I will be uploading the final product to a CentOS server.
I have this line of code:
$dbFunc->GetTemplateName($report['uat_template'])[0]['uat_name'];
On the Windows configuration, this works fine. However I get a parse error on the Unix box. It isn't happy with the [0] identifier.
If i do;
$temp = $dbFunc->GetTemplateName($report['uat_template']);
$temp = $temp[0];
echo $temp['uat_name'];
It works fine, but it's added 2 lines of code?
Is there any way to make the first way work on Unix?
Thanks
The first example in your question makes use of array dereferencing feature in PHP, which is only available for PHP 5.4+. If you're using an older PHP version, you will need to upgrade. If upgrading is not an option, you're stuck with the second method, I'm afraid.
In PHP 5.4 PHP added a function array dereferencing has been added, e.g. foo()[0].
So it looks like on CentOS you have >=PHP5.4 and on XAMPP <5.4
More info : http://php.net/manual/en/migration54.new-features.php

Closures with PHP

I have a weird problem trying to use closures in PHP. When assigning a closure to a variable, I get a null value. But when displaying the closure with var_dump(), everything is alright.
Here is a source code that summarizes the problem:
$f = function() {};
var_dump($f); // 'null'
var_dump(function() {}); // 'object(Closure)[1]'
I'm using PHP 5.3.1.
Edit: I forgot to mention, I have this problem only when I'm using PHP via Apache. I don't have issues when using PHP CLI.
A colleague found the answer to the problem: the responsible is eAccelerator! Apparently it's not compatible with PHP 5.3 closures... (source)
Disabling it solved the problem.
Thanks for your help!
It's either a very rare (and already fixed) bug or you're not showing the exact same usage that gives you a NULL. My guess is that you're doing this with the first var_dump():
var_dump($f());
Note the parenthesis, they cause the function to be run and therefore you get its return value.

PHP - Problem with scalar value and memory leak

I am dealing with a very strange situation that has to do with the Warning: Cannot use a scalar value as an array and memory leak.
The script is very simple and I can not figure out the problem.
Code
$variants=array();
if($text)
{
$v=explode(",",$text);
if(is_array($v) && sizeof($v)>0)
{
foreach($v as $i=>$part)
{
$tmp=explode(":",$part);
list($thekey,$thevalue)=$tmp;
//$variants=array();
echo "<div>TYPE==".gettype($variants)."</div>";
echo $variants[$tmp[0]]=$tmp[1];
}
}
}
If I run the code above as stand alone is working fine. But when put it in my framework as small part behave very strange. I got a Warning: Cannot use a scalar value as an array and in order to solve it I added
$variants=array();
on the first line. When running the script the gettype returns ��� the first time and after that return integer.
If i uncomment $variants=array(); just before the gettype, it works. But of course I don't to get the whole array, only the last record return.
I parse my code to find out that the variables I use are declared before I even change all the variable names to stupids but no luck.
Trying to debug and tune the code where times that when running the script instead of see something in the screen the browser download the script instead and some other times I had memory leaks.
Can anyone point where or what to look for, or debug it and solve it?
Problem solved
Before run the code i was calling a function
$obj->draw($$id)
That was causing the problem
The solution
$value=$$id;
$obj->draw($value)
I dont know why this causing the problem.
If anyone has a theory please post it.

Warning "Variable does not seem to be used in its scope" in foreach

I wrote a simple foreach to filter one array and write it's results to another array.
foreach ( $users as $user ) {
...
$user_email_list[ ] = $user[ "intranet-id" ][ "value" ];
...
}
Problem is that NetBeans is showing me a warning "Variable does not seem to be used in its scope" on $user. If I try to initialize $user before foreach, no warning shows. However it doesn't seem to be right.
Is this a NetBeans bug or do I simply misunderstand anything?
I am using NetBeans version 6.7.1 (Build 200907230233) and PHP 5.2.10.
Netbeans might be getting it wrong - the code seems ok.
Code looks fine to me and I have seen Netbeans make similar mistakes before so I wouldn't worry about it.
Interestingly tho, I just copied your code into Netbeans (I am using the exact same version and build) and didn't get a warning
Your code looks fine.. This happens often in NetBeans. Taking one second to look at it and make sure it's wrong is what I usually do. Sometimes closing and re-opening the file makes NetBeans re-parse the file and figure things out. I've also seen re-starting NetBeans do it but that's a pain every time this happens.

Categories