tags:

views:

70

answers:

2

For example, take this code:

$ch = curl_init($resultSet['url']."?get0=get0&get1=".$get1."&get2=".$get2."&get3=".$get3);

This of course, looks very ugly, and kind of a pain in the ass to read. So my question is, would I be able to use something like this:

$allgets ="?act=phptools&host=".$host."&time=".$duration."&port=".$port;
$ch = curl_init($resultSet['url'] . $allgets);

Very simple question I suppose, but my server is undergoing maintenance, so I can't upload it and test it myself. I suppose a yes or no answer will suffice, but if you have a more efficient way of doing this, that would be even better. :)

+2  A: 

Definitely, it's just string concatenation.

You could also take a look at string variable parsing if you want it to be "less messy".

Chris Doble
So, my second example will work without error?
Rob
Yea, in theory yes.
Martti Laine
+1  A: 

Your example might not work depending on the value of the variables because you didn't use URL-encoding.

A better way is something like this,

$fields = array (
   'host' => $host,
   'port' => $port
);

$ch = curl_init($resultSet['url'] . '?' . http_build_query($fields));
ZZ Coder
I would go one further and use `http_build_url` as well then it would be easy to change things if the url needs changing
SeanJA
If url may contain query strings already, http_build_url() is definitely the way to go. However, it requires http extension, which is rarely installed on all the hosts I used.
ZZ Coder