0

I have made a code that works however it doesn't work when I try to echo a variable in the json/array. Can anyone help?

Code that works

$data = array("ips" => ["ip" => "1.1.1.1"]);                                                                    
$data_string = json_encode($data);  

Code that doesn't work

 $test=1.1.1.1
 $data = array("ips" => ["ip" => "$test"]);                                                                    
 $data_string = json_encode($data);  

The code just return with 500error on browser

As you can see i am trying to introduce a variable. Can anyone help?

3
  • 2
    The error 500 is most likely caused by $test=1.1.1.1, the rest of your code is FINE You need to add string quotes around the $test variable, such as: $test = '1.1.1.1' Commented May 16, 2020 at 11:21
  • The posted answer is correct. I'll also add that "$test" is the same as simply $test and the latter looks a lot cleaner. Also you're mixing up styles. array() is the old style and [] is the newer style. I suggest you stick to one or the other (and the newer style is a lot cleaner). So - $data = ["ips"=>["ip"=>$test]]; Commented May 16, 2020 at 11:23
  • put quote around "1.1.1.1" Commented May 16, 2020 at 11:42

3 Answers 3

1

You have syntax error in $test variable. You can use string type, like this:

$test = '1.1.1.1';
$data = array("ips" => ["ip" => "$test"]);                                                                    
$data_string = json_encode($data);  
Sign up to request clarification or add additional context in comments.

Comments

1

you need to put quotes around "1.1.1.1", or php's parser doesn't interpret it as a string (which is what it's supposed to be)

DO THIS:

$test="1.1.1.1";
$data = array("ips" => ["ip" => "$test"]);                                                                    
$data_string = json_encode($data);  

NOT THIS:

$test=1.1.1.1
$data = array("ips" => ["ip" => "$test"]);                                                                    
$data_string = json_encode($data);  

Also it's bad practice to mix the shorthand for arrays with the array function, you should either do array("ips" => array("ip" => "$test")); or ["ips" => ["ip" => "$test"]];

1 Comment

Oh shit, i am so stupid. I was focusing on the $data that i completely missed the first one.
1

While the right answers are already given: You have a syntax error there and should write

$ip = "1.1.1.1";

I would recommend that you first take a look at your server's error logs for the web server and PHP. It will clearly state why your script is failing:

[Sat May 16 13:26:20.744739 2020] [proxy_fcgi:error] [pid 1653:tid 140650782840576] [client ::1:55866] AH01071: Got error 'PHP message: PHP Parse error:  syntax error, unexpected '.1' (T_DNUMBER) in /var/www/html/stackoverflow.php on line 2\n', referer: http://localhost/

Alternatively you can use php myScript.php and run this from the console and get the same error message. Naturally, the possible use of the PHP-CLI depends a bit on the complexity of your script.

This will help yourself in the future to find those errors easier.

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.