Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅包括从一个php文件到另一个php文件的变量_Php_Html - Fatal编程技术网

仅包括从一个php文件到另一个php文件的变量

仅包括从一个php文件到另一个php文件的变量,php,html,Php,Html,我在url.php中有一个变量。 我想在多个其他文件中访问它。我试图在所有这些文件中包含url.php。但有一个问题 <?php include 'url.php'; //include url.php echo "My id is : $myid"; ?> <!DOCTYPE html> <html> <head> <!-- AddThis Smart Layers END --> </head> //somec

我在
url.php
中有一个变量。 我想在多个其他文件中访问它。我试图在所有这些文件中包含
url.php
。但有一个问题

<?php
 include 'url.php';   //include url.php
 echo "My id is : $myid";
?>
<!DOCTYPE html>
<html>
<head>

<!-- AddThis Smart Layers END -->
</head>
//somecontent
<body>

你不能这样做。唯一的方法是删除HTML并将其包含在其他地方。调用include打开文件,然后对内容运行
eval
。如果它包含站点的重要UI部分,您应该重新考虑您的设计模式。

在包含
url.php
之前,您可以在每个php文件中声明一个变量 像这样:
$uservar=1

然后在url.php中,将html块放入if语句中

如果($usevar!=1){

HTML块

}

您可以在那里设置一些条件,如isset()。
You can put some condition there like isset().
<?php
 if(isset($_POST['myid']))
  {
   $myid=$_POST['myid'];
   echo "myID is : <br>";
   echo $myid;
  }
 else{
?>
 <html>
  <head>
   <html xmlns:fb="http://ogp.me/ns/fb#">
  </head>
    //some othe html stuff
 </html>
 <?php } ?>
//还有一些html的东西
另一种解决方案是,您可以在会话中保存变量,以后还可以在其他页面中检索该值

url.php

<?php
$myid=$_POST['myid'];
session_start();
$_SESSION['my_id']=$myid;
?>

您可以像这样使用变量。 useMyId.php``

<?
session_start();
$var=$_SESSION['my_id'];

echo "Here is the variable".$var;
?>


@subzero:我无法删除它。它包含我的网站的用户界面!我真的很喜欢评论的魔力:
include'url.php'//include url.php
即使在此之后,如果我包含上述代码,它也将包含html。如果未设置$\u POST['myid']变量,则它将包含html。你可以修改包含条件。你是否在每页都有$u POST['myid']变量?聪明的答案。但是在我的页面上有一个正在进行的会话,它以
session_start()开始。有可能发生碰撞吗?像session1和session2这样的东西是分开启动的吗?如果会话已经在页面上启动,则无需添加会话_start();但请确保会话必须在分配$_session['my_id']=$myid之前启动;$var=$\u会话['my\u id'];情况并不复杂。包含上述内容的页面由ajax请求加载到index.php中。php有一些使用session_start()启动facebook登录状态的代码。
<?
session_start();
$var=$_SESSION['my_id'];

echo "Here is the variable".$var;
?>