简体   繁体   中英

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

...

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

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. eg

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM