我有一个函数来发出代码的回声,所以我不必每次都重复它:
function echo_snippets($connection, $query, $snippet, $variable_1, $variable_2, $variable_3, $variable_4, $variable_5, $variable_5){
$result = $connection->query("CALL $query");
if($result){
while ($row = $result->fetch_object()){
include 'includes/snippets/'.$snippet.'';
}
$result->close();
$connection->next_result();
}
}
然后我从页面调用该函数:
<?php
echo_snippets($connection,'index_php_Query_one()', 'echo_things.php', '', '', '', '', '', ''); ?>
// Empty spaces are for variables I can use sometimes.
?>
然后在/includes/snippets/echo_things.php的包含文件中我有变量和echo:
<?php
$first_thing = $row-> first_thing;
$second_thing = $row-> second_thing;
echo '<div>This is the first_thing: '.$first_thing.'
and the second_thing:'.$second_thing.'</div>';
?>
一切正常。但是我在函数内部包含了url的问题。我不能使用相对url因为那时我无法从目录中调用该函数。我在MAMP的本地目录中工作,由于某种原因它不接受绝对地址:
include 'http://localhost:8888/includes/snippets/'.$snippet.'';
我找到的唯一解决方案是在调用函数
时传递绝对URL echo_snippets($connection,'index_php_Query_one()', '../includes/snippets/echo_things.php', '', '', '', '', '', ''); ?>
但是我想知道有一种方法可以给这个函数一个绝对的url。
一个。
答案 0 :(得分:0)
尝试:
getcwd()
这将给出:
/Applications/MAMP/htdocs/your_project_dir