5

I'm new to php and not sure why the go-daddy server treats JSON data differently. When I host it locally on my computer in local host, when I want to echo empty JSON array, I simply put [] shown as below. But when I uploaded the code to go-daddy server and try it out it echoed an error, Parse error: syntax error, unexpected '[', expecting ')' in...I'm wondering how can I put the JSON so it can echo [] when needed. Otherwise it will give "null" and when it parse into AS3, it turn into a JSON parse error.

if (!empty($output)){
        echo json_encode( $output );}
        else{
            echo json_encode( [] );
        }
0

4 Answers 4

7

That is because your webserver version must be less than 5.4. You are trying to use a new feature of PHP 5.4 called the short array syntax

Use echo json_encode(array()); instead of echo json_encode( [] );

Working Demo on PHP v 5.3

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the info, i just went to the server and updated the version, now it works, i didnt know it was set to 5.3 before.
4

You have to use this:

if (!empty($output)){
        echo json_encode( $output );}
        else{
            echo json_encode(array());
        }

Comments

3

[] for arrays is only supported in php 5.4+.

So that means php is not a high enough version on your host, try asking them to upgrade to 5.4 or try array(); instead of []

Comments

2
if(count(json_encode($jArrary,1))==0) {
echo "empty";
}
//or
if(empty(json_encode($jArrary,1))) {
echo "empty";
}

you can use this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.