I migrated my web application from Symfony 2 to 3. Since then whenever a foreach loop is executed on null, an error is thrown ("Invalid argument").
Example:
$myResults = null;
foreach ($myResults as $item) {
// do something
}
How do I achieve that as before no error is thrown?
This is a bug introduced by an upgrade to a newer PHP version (7.1+).
You can verify this by executing your code sample in an interactive php shell without any Symfony code being loaded:
$ php -a
Interactive shell
php > $var = null;
php > foreach ($var as $v) {}
PHP Warning: Invalid argument supplied for foreach() in php shell code on line 1
You can also verify this on 3v4l.org
In order to fix this you have to check for null before looping or (temporarily) go back to an older (unsupported!) PHP version. Depending on how well your code is typed there are some static code analysis tools, that will help you finding the loops you will have to safeguard.
Related
I am currently working from home (on a Sunday!) and I am trying to figure out why my Perl script is returning NULL to PHP from where it is called. However, I don't see how I can debug the Perl script itself. The PHP file returns a warning that I am trying to do an array operation on a non-array object (because the expected array is actually NULL returned by PHP). The logs of the webserver have only logged this warning as well - no Perl errors.
Is there a place where specific 'external' logs are stored on a server? Or, is there a better way to debug a Perl file that is been run from a PHP file that is required in a main PHP file? Debugging isn't necessary (I don't need a debug mode) but I'd like to see the errors or warnings at least.
You can add following code at the top of your Perl script:
sub debug_log
{
open my $log_fh, ">>", "/tmp/debug.log";
print $log_fh $_[0];
warn $_[0];
close $log_fh;
}
$SIG{__WARN__} = \&debug_log;
$SIG{__DIE__} = \&debug_log;
This way all the warnings and die messages should end up in /tmp/debug.log.
So, here is a small code snippet from my project
if (!empty($user['IndustryUser'])) {
$user['IndustryUser'] = array_filter($user['IndustryUser'], function ($ku) {
$k = !empty($ku['Industry']['name']) ? trim($ku['Industry']['name']): '';
return $k != '';
});
$userIndustries = array();
foreach ($user['IndustryUser'] as $ku) {
$userIndustries[] = $ku;
}
$user['IndustryUser'] = $userIndustries;
}
This code is present inside a Model named User.php. And also there is a shell script named FetchIndustriesShell.php inside app/console/command, which makes use of this Model.
When I run this script from the command line, as cake FetchIndustries, I get the following error
Parse error: syntax error, unexpected T_FUNCTION in D:\wamp\www\industry-svn\
app\Model\User.php on line 203
where Line 203, is where the anonymous function is defined(line 2 from code snippet).
What i tried is
1) Run phpinfo() inside bootstrap.php to verify the php version. It shows PHP 5.5.12 (attached is the screenshot).
2) I installed xampp,to verify if my wampp installtion/php libraries had problem. But I still got the same error.
3) Also referred https://github.com/cakephp/debug_kit/issues/134. But that did not help either.
CakePHP Version used is: 2.5.1
Project currently running on: wampp (local)
I am writing a function in PHP, and in this function, I'm trying to set this up so that I can have the script call the function one way if the server is using PHP 5.6, and another way if it is on an older version. However, when I attempt this on a lower version of PHP, it results in a syntax error. I was wondering if there was any way to force PHP to ignore the new syntax entirely rather than throwing an error if it is using an older version so that I can use this script between versions. Here is the code that I am trying to do this with:
if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
// This function requires PHP 5.6+
$stmt->bind_param($type, ...$args);
} else {
// For older versions (slower)
call_user_func_array([$stmt, 'bind_param'], array_merge(array($type), $args));
}
Syntax parsing runs on a different level than runtime parsing. PHP will detect the syntax error before the script even gets to run and your if be evaluated.
A possible solution would be as Ja͢ck suggests, use two script files and conditionally include one based on the version.
I use this to compile my java file
$command_compile = "javac $target_path_file 2>&1";
exec($command_compile, $compile_result, $return_compile);
And When my java file has syntax error. It always has error
Error: Could not find or load main class Myclass
How to get error such as Syntax Error. need ) On line 5
Thanks you
$res = exec($command_compile, $compile_result, $return_compile);
inside $res you get the last line of output of the executed command
If javac returns more lines (either error or informative messages) you may want to parse the $compile_result array that contains every output line.
foreach($compile_result as $compile_result_line) {
// do what you need with $compile_result_line
}
Anyway see http://php.net/function.exec for a detailed explanation of exec
The error you get is not from PHP code but is a "java" issue when you call javac. It seem you're trying to compile a file that reference an undefined class.
I am using Symfony task php5.2. Here is a portion of my code:
array of images
foreach($array as $k=>$v)
{
abc(); // function call which will cope images from one server to another
// by using file_get_content in a php variable and using api(wso).
echo memory_get_usage();
}
problem is memory_get_usage(); always returning same value but when i am using top command memory is increasing nonstop.
Is there any bug in symfony task or php5.2 or wso.
Have you tried memory_get_usage(true)?
Also, have you considered that it might not be PHP that is using the memory? But some other library you are using?