简体   繁体   中英

Turning a Block of PHP into a Variable

I have some PHP that I have to add to my page a couple times and instead of just pasting it a bunch of times I would like to turn it all into a php variable. I have searching but don't think I know the correct term to search for to achieve this.

Here is what I'm trying to turn into 1 variable:

session_start();
$time = time();
$hash = md5($key . $time);
$str = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = str_replace("0239", "738", $str);
header("Location: http://google.com");
exit;

Don't turn it into variable, but into a function.

function functionName($key) {
  session_start();
  $time = time();
  $hash = md5($key . $time);
  $str = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  $url = str_replace("0239", "738", $str);
  header("Location: http://google.com");
  exit;
}

Now you can anywhere in code call

funcitonName($key);

to execute your code.

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