Ferryboi
Ferryboi

Reputation: 119

API - PHP Variables

We are trying to get part of a API Working and we are getting a litle stuck

$armory = new BattlenetArmory('EU','Azjol-Nerub'); //ex. ('US', 'Exodar')

Above is part of the code within the Api script and what we are looking for is a way to "populate" the EU and Azjol-Nerub parts dynamically so hopefully we could have another page feeding the 2 variables into this script.

Hardcoded the script works without a sniff of a problem .... However

I dont know if this even works but this is what i ventured as a newbie and tried :

$test='EU'; 
$armory = new BattlenetArmory('.$test.','Azjol-Nerub'); //ex. ('US', 'Exodar')

$test='EU'; 
$armory = new BattlenetArmory('<?php echo $test ?>','Azjol-Nerub'); //ex. ('US', 'Exodar')

And it broke

Im not too sure how to get around this .. even if there is a way to get around it

Im hoping someone might be able to lend me a hand with this if possible and for me to possibly learn as to where im going wrong

thanks

Upvotes: 1

Views: 105

Answers (3)

Enigma Plus
Enigma Plus

Reputation: 1548

Just to clear up the understanding here, dots are for concatenating strings. Like + in JS and & in VB.

$name = 'John';
echo 'hello there ' . $name . ', how are you?';

Weirdly in PHP you can put variables inside strings that are enclosed by double quotes and they will be replaced by values - so this will work (with a very slight performance drop).

$name = 'John';
echo "hello there $name, how are you?";

The thing was never going to work, PHP loads up all code between those tags at the start, parses them and then begins to do its job. It has no meaning after that.

So the answer to your question is as someone else said above - just thought I'd explain:

$test='EU'; 
$armory = new BattlenetArmory($test, 'Azjol-Nerub'); //ex. ('US', 'Exodar')

Upvotes: 0

pho
pho

Reputation: 25489

You need to specify the variable OUTSIDE the quotes

$armory = new BattlenetArmory($test,'Azjol-Nerub');

Or, if you have mod_string_replace enabled, you can do

$armory = new BattlenetArmory("$test",'Azjol-Nerub'); 

This will replace the $test inside the double quotes with the value of $test. This is unnecessary, because it will add useless processing requirements to your page, but it is useful if you want to create a string out of many variables. e.g.

$name="John Doe";
$age=35;
$country="USA";

$message="$name, age $age, lives in $country"; //Gives John Doe, age 35, lives in USA

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798616

...

$test='EU'; 
$armory = new BattlenetArmory($test, 'Azjol-Nerub'); //ex. ('US', 'Exodar')

Upvotes: 3

Related Questions