0

I am trying to pass the app_id and app_key in the response string. I am setting those as constants and then passing them in the string. I am getting syntax error

Parse error: syntax error, unexpected ':'

What am i doing wrong? sorry I am pretty new to PHP

    $app_id = 123456; 
    $app_key = '1234abcd5678';

    $query_string = https://apiresponse.htm?app_id=$app_id&app_key=$app_key;

    echo $query_string;
Albzi
  • 15,431
  • 6
  • 46
  • 63
soum
  • 1,131
  • 3
  • 20
  • 47

4 Answers4

3

You need to surround with quotes your string and concatenate your variables to avoid the error

$query_string = 'https://apiresponse.htm?app_id='.$app_id.'&app_key='.$app_key;
Fabio
  • 23,183
  • 12
  • 55
  • 64
2

... But $app_id and $app_key are not constants, they are usual scalar variables (!). So to express your $query_string the best thing is

$query_string = "https://apiresponse.htm?app_id=$app_id&app_key=$app_key";

more fast and readable than concatenation.


Use define() to express constants:

  define('app_id','123456');
  define('app_key','1234abcd5678');
  $query_string = 'https://apiresponse.htm?app_id='.app_id.'&app_key='.app_key;

PS: now the best way the concatenation operator, like the @Fabio's suggestion.

Community
  • 1
  • 1
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
1

Use " for replace vars to values in string

$query_string = "https://apiresponse.htm?app_id=$app_id&app_key=$app_key";

It is cleaner than string concat (like 'a='.$a)

Max
  • 1,824
  • 13
  • 22
1

Your $query_string is not enclosed by single- or doublequotes.

In this case, as your string contains variables you should use doublequotes. That way, you don't have to break out of the string to add the variables.

$query_string = "https://apiresponse.htm?app_id=$app_id&app_key=$app_key";

If you were to use singlequotes, you would have do this:

$query_string = 'https://apiresponse.htm?app_id='.$app_id.'&app_key='.$app_key;

Read more on PHP Manual for Strings

Sven
  • 5,155
  • 29
  • 53