我们正在尝试让API的一部分工作,我们遇到了一点卡住
$armory = new BattlenetArmory('EU','Azjol-Nerub'); //ex. ('US', 'Exodar')
以上是Api脚本中的一部分代码,我们正在寻找的是一种动态"填充"EU和azjll - nerub部分的方法,因此希望我们可以有另一个页面将这2个变量输入该脚本。
硬编码的脚本工作没有任何问题....然而
我不知道这是否有效,但这是我作为一个新手冒险尝试的:
$test='EU';
$armory = new BattlenetArmory('.$test.','Azjol-Nerub'); //ex. ('US', 'Exodar')
$test='EU';
$armory = new BattlenetArmory('<?php echo $test ?>','Azjol-Nerub'); //ex. ('US', 'Exodar')
它破了
我不太确定如何绕过这个…即使有办法绕过它
如果可能的话,我希望有人能帮我一把,让我知道我错在哪里
谢谢
…
$test='EU';
$armory = new BattlenetArmory($test, 'Azjol-Nerub'); //ex. ('US', 'Exodar')
您需要在引号外指定变量
$armory = new BattlenetArmory($test,'Azjol-Nerub');
或者,如果您启用了mod_string_replace,您可以执行
$armory = new BattlenetArmory("$test",'Azjol-Nerub');
这将用$test的值替换双引号内的$test。这是不必要的,因为它会向页面添加无用的处理需求,但是如果您想要用许多变量创建字符串,那么它是有用的。例如
$name="John Doe";
$age=35;
$country="USA";
$message="$name, age $age, lives in $country"; //Gives John Doe, age 35, lives in USA
只是为了澄清这里的理解,点用于连接字符串。比如JS中的+和&在VB .
$name = 'John';
echo 'hello there ' . $name . ', how are you?';
在PHP中,你可以把变量放在用双引号括起来的字符串中,它们将被值所取代——所以这是可行的(性能下降很小)。
$name = 'John';
echo "hello there $name, how are you?";
这个东西永远不会工作,PHP在开始时加载这些标签之间的所有代码,解析它们,然后开始工作。之后就没有意义了。
所以你的问题的答案就像上面别人说的那样——我只是想解释一下:
$test='EU';
$armory = new BattlenetArmory($test, 'Azjol-Nerub'); //ex. ('US', 'Exodar')