Php error on new server - php

Hi i have a new server runing php 5.4 on rackspace and there is errors con my code
i cannot do this becouse its giveme a error:
if(empty($basics->conversions*100) || empty($basics->activations))
this is the problem: ($basics->conversions*100);
i have to do $vr = $basic->conversions*100;
if(empty($vr)) but i have this all over my code and i cannot fix it
Parse error: syntax error, unexpected '*', expecting ')' in ***
the other error is when using a function returning an array and accesing that array insted of assign it to a variable example:
getReports($date, $todate)['utm'];
this gives me error.
but if i do:
$arrReportes = getReports($date,$todate);
$arrReports['utm'];
Works perfectly why ? can you helpme i cannot find any on google

From http://php.net/manual/en/function.empty.php
Prior to PHP 5.5, empty() only supports variables; anything else will
result in a parse error. In other words, the following will not work:
empty(trim($name)). Instead, use trim($name) == false.
This explains why your first error is occurring - empty() just doesn't let you pass an expression to it like you are doing in the version of php you said you are running (5.4).
The second error should really have been put in a separate question. In theory what you are trying to do should be possible since php 5.4 -
As of PHP 5.4 it is possible to array dereference the result of a
function or method call directly. Before it was only possible using a
temporary variable.
(from http://php.net/manual/en/language.types.array.php)
- but you claim to be running 5.4 so I'd imagine there's an issue somewhere else. I'd double check the version of php you're running, and ensure your code isn't at fault there too. You didn't specify what error you were getting, so it could well be that the returned array is null, or anything.

Related

Php anonymous function with short array not work in 5.4.35

I have an odd issue with this php script
<?php
$expr = '["Nizky_tarif","Vysoky_tarif"][0 == 1]';
$newfunc = #create_function('', "return $expr;");
var_dump($newfunc);
echo $newfunc();
On server it crashes with error:
bool(false)
PHP Fatal error: Function name must be a string in /tmp/foo.php on line 7
On localhost it works:
string(9) "\000lambda_1"
Nizky_tarif
Server php version is 5.4.35, localhost 5.6.3. However I don't see why it wouldn't work, the short array syntax was added in 5.4, right?
ps. Just tried, if I change it to the old array syntax, it crashes also.
In the expression ["Nizky_tarif","Vysoky_tarif"][0 == 1] the code is trying to directly dereference an array literal. This is a feature which was introduced with PHP 5.5.
With PHP 5.4 this expression is not valid, resulting in the error "Function name must be a string".
You could change that into to parts to make it work:
$data = ["Nizky_tarif","Vysoky_tarif"];
return $data[0 == 1];

PHP gives unexpected [ - but why?

having strange trouble with this:
date_parse('2014-08-26')['hour']
It works on a php command line.
But this throws an error:
if(date_parse('2014-08-26')['hour']){ echo "works";}
syntax error, unexpected '['
Why is that? The result should be any number between 0 and 23 or nothing, nothing would it be in this case.
EDIT: yes, I am on PHP 5.3.14
Please note that versions of PHP older than 5.4 can not access array attributes from a functions return value directly. You have to store the functions return value in a new variable and access the array from that variable.
So this code
if(date_parse('2014-08-26')['hour']){ echo "works";}
only works on PHP 5.4 and higher. If you run a PHP version lower then 5.4 your code should look like this:
$parseResult = date_parse('2014-08-26');
if($parseResult['hour']) { echo "works"; }
You must be using below 5.4 version of PHP. If so than please use the latest version of php as the version below 5.4 does not allow brackets [].
Before version 5.4, PHP didn't allow you to dereference elements of arrays returned from function calls without using a temporary variable:
http://php.net/manual/en/language.types.array.php#example-102
$cond = date_parse('2014-08-26')['hour'];
if($cond)
{
echo "works";
}
Hope this works

Parse error: unexpected '#' symbol in constant function in php

In my project, when I try to install a software, I got an parse error in last step of installation
The parse error is
Parse error: syntax error, unexpected '#' in
/path/to/server/subfolder1/projectfoldername/subfolder/filename.php
on line 21
The coding in that particular line of that file is
if(#constant($matches[1][0]) != #$matches[1][0]){
if(!empty(#constant(#$matches[1][0])) & !empty(#$matches[0][0]) & !empty(#$design_m_r[$key])){
$design_m_r[$key] = #str_replace($matches[0][0], constant($matches[1][0]), $design_m_r[$key]);
}
}
Our site php version is php 5.3.28. I tried to search for this error. But I dont get any solution for this. Some of the forums told about this error as "This is the advanced php version functions. So this should not support for php 5.3.28 version". But when I searched, there is no versions used this type of function.
You can't use the # error suppression operator like that.
From the PHP Docs..
The #-operator works only on expressions. A simple rule of thumb is:
if you can take the value of something, you can prepend the # operator
to it. For instance, you can prepend it to variables, function and
include calls, constants, and so forth. You cannot prepend it to
function or class definitions, or conditional structures such as if
and foreach, and so forth.
Also, passing arbitrary expressions on empty is allowed only from PHP 5.5 .

Cakephp throws an error unless array is devided

So i have an Api call where i get a json array:
When i do the following:
$data = $this->HasOffers->get_full_detail_report()['data']['data'];
$this->set('data',$data);
i get an error saying an internal error has occoured
However if i do:
$data = $this->HasOffers->get_full_detail_report();
$data2 = $data['data']['data'];
$this->set('data',$data2);
everything is working correctly.
Now my question is why is this happening? and how can i fix it?
The syntax you are using in the first example is only available in PHP >= 5.4. See relevant section of PHP manual: http://php.net/manual/en/language.types.array.php#example-88
You can see an example running in different versions of PHP at: http://3v4l.org/XhCKH
Your CakePHP site likely has error reporting turned off so, rather than displaying the syntax error, it is displaying an Internal Error.
I'm guessing you have debug < 2, so the description of the error is not very detailed. However, that behaviour is known to be a PHP < 5.4 issue (post regarding that subject).
To "fix" it, you need to upgrade PHP to 5.4 at least. Or, just use an intermediary variable for those cases, it's not that bad.
This is happening because the array you are referencing in the first example only exists after the function get_full_detail_report() is called. PHP does not like this. PHP wants your array to exists before you reference it. I assume that it attempts to locate any variables within your statement before performing any operations, which would mean it is searching for an array that does not exist until it performs those operations.
If anyone has any more insight into this, I would welcome their revisions / comments.

PHP Fatal Error. Does empty() try to alter the results passed into it?

Ran into a strange problem in PHP today and I'm wondering if someone can explain it. While comparing two arrays I initially tried something like this:
echo empty(array_diff( array('foo','bar') , array('bar','foo') ))
This results in the following error:
Fatal Error: Can't use function return value in write context
Rewriting this as...
$dif = array_diff( array('foo','bar') , array('bar','foo') );
echo empty($dif);
...works perfectly. Empty should just be evaluating the value passed in to it, not writing to it, so what's going wrong here? Tested in both PHP 5.2.10 and PHP 5.3.2.
I've resolved the issue by using !count() instead of empty(), but I'm curious why it doesn't work in the first place. Is empty() trying to alter the result from array_diff?
Check the manual on empty():
Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
empty(), like e.g. echo() and die(), is a language construct, and therefore has different rules than a normal function (in which your example would work fine).

Categories