user6116249
user6116249

Reputation:

add variable to url file get php

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.

Upvotes: 1

Views: 56

Answers (3)

Tom
Tom

Reputation: 596

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.

Upvotes: 0

xCRKx TyPHooN
xCRKx TyPHooN

Reputation: 808

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';

Upvotes: 0

Eric
Eric

Reputation: 10646

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

or you could not declare it as array

$commsIP ='192.168.1.1';

Upvotes: 1

Related Questions