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,201
  • 5
  • 43
  • 61
soum
  • 1,113
  • 3
  • 18
  • 46

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
  • 22,442
  • 12
  • 51
  • 63
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
  • 12,407
  • 21
  • 149
  • 275
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,744
  • 12
  • 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

Svenskunganka
  • 4,806
  • 29
  • 53