1

I have a variable define

$commsIP = ['192.168.1.1'];

I am trying to add it to a url

$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/$commsIP");

but I get the following error

Notice: Array to string conversion

but if I put the link like so

$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/192.168.1.1");

It displays fine.

1
  • If it is a single value, then why do you make an array out of it in the first place? Remove those square brackets! Commented Apr 12, 2016 at 21:24

3 Answers 3

1
$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/".$commsIP[0]);

or you could not declare it as array

$commsIP ='192.168.1.1';
Sign up to request clarification or add additional context in comments.

Comments

0

You defined the variable as an Array, which is why it's saying it can't convert from an Array to a string. Placing []'s around the variable signifies that it's an array.

Just remove the []'s and it will work fine.

$comssIP = '192.168.1.1';

Comments

0

You put brackets around the IP address, when you do this it has the same functionality as an array.

If you change this:

$commsIP = ['192.168.1.1'];

To this:

$commsIP = '192.168.1.1';

It will work.

Alternativly you can also do this:

$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/{$commsIP[0]}");

When you do that it will get the first result out of the $commsIP array.

Comments

Your Answer

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