[英]PHP pass variable to include from the include call
是否可以在包含调用时将变量传递给包含,然后在包含文件中使用该变量?
例如这样的:
// include this somewhere
<?php include 'includes/contact_form.php', $platform='desktop' ?>
// and this somewhere else
<?php include 'includes/contact_form.php', $platform='mobile' ?>
// then inside the contact_form.php file use them like
<input id="name_<?echo $platform ?>" type="text">
<input id="email_<?echo $platform ?>" type="email">
// which would result in:
<input id="name_desktop" type="text">
<input id="email_desktop" type="email">
// and
<input id="name_mobile" type="text">
<input id="email_mobile" type="email">
您可以在包含之前定义一个平台变量,并且包含的文件将可以访问它:
index.php
<?php
$platform = 'desktop';
include 'contact_form.php';
?>
contact_form.php
Platform is: <?php echo $platform; ?>
<input id="name_<?php echo $platform; ?>" type="text">
<input id="email<?php echo $platform; ?>" type="email">
// 编辑
由于您特别提到“在包含调用中”,我想我会提到它可以做到:
include 'http://example.com/contact_form.php?platform=desktop'
但是,如果您想动态更改平台,您仍然需要 (1) 定义平台变量和 (2) 使用这种远程格式需要启用allow_url_include 。 您可以在include的手册页中阅读有关此内容的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.