所以你可以在这里看到我已经声明了我在整个代码中使用的几个变量。我将其余部分剪掉了,但是有些情况下你看到我使用global来引用上面描述的变量。我的任务要求我们不要使用全局变量,并且似乎无法侦察互联网以找到替换全局但仍然能够使用所述变量的任何潜在解决方案。有什么想法吗?
//variables
$movie = $_GET["film"]; //dynamically get film name via link
$contents = file_get_contents("$movie/info.txt"); //get info of said film from /info file in my docs (name, rating, year)
$info = explode("\n",$contents);
$sidecontents = file_get_contents("$movie/overview.txt"); //get all the credits (producer, ratings, etc) of film that is displayed below image
$b = explode("\n",$sidecontents); //for every new line (in the txt they're in same line but theres the line break), split each string by string
$j = 0; //variable to use as a counter
//rotten or fresh icon next to rating for movie
function percentLogo($inf)
{
//if percentage of film is more than 60 then print fresh tomato
if($inf >= 60)
{
?> <img src='freshbig.png' alt='Fresh'/>
<?php
}
//else, rotten tomato lol self explanatory but yeah
else
{
?> <img src='rottenbig.png' alt='Rotten'/>
<?php
}
}
//info on the right sidebar of the page (not including the picture)
function sideinfo()
{
global $b;
foreach($b as $credits) //for each loop to increment through b (which is all the content we split for each line break) and store it in credits dynamically.
{
$credits = explode(":",$credits); //we then split the string up for everytime we see a : and store it in credits again (bad programming practice but it's late so whatever lol)
//essentially print out wh
答案 0 :(得分:0)
虽然这是一项任务,但我不会为你做这项工作。然而,向正确的方向推进通常可以解决问题。测试的想法是如何正确使用函数。
function getMovieInfo(){
$contents = file_get_contents($_GET["film"] . "/info.txt");
# This variable is defined in a function, it is a local variable and not global.
return explode("\n",$contents);
}
print_r(getMovieInfo());
不是将值存储在变量中,而是将其返回。 在这种情况下,您返回一个数组,但您可以处理函数中的信息以返回特定的内容。
使用函数参数可以使它更具动态性:
function getMovieInfo($file){
return explode("\n",file_get_contents("$file/info.txt"));
# -------------------------------------^ nameofmovie/info.txt
}
print_r(getMovieInfo("nameofmovie"));