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?
$test=1.1.1.1, the rest of your code is FINE You need to add string quotes around the$testvariable, such as:$test = '1.1.1.1'"$test"is the same as simply$testand 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]];