Php 通过include调用函数
如何将一页包含到另一页中 例如,第1页包括第2页。第2页在获得正确的参数之前不会显示任何内容 这是第2页的直接url调用Php 通过include调用函数,php,Php,如何将一页包含到另一页中 例如,第1页包括第2页。第2页在获得正确的参数之前不会显示任何内容 这是第2页的直接url调用 www.mypage.com/execute.php?cmd=test 输出是hello world 但是当将同一页包含到第1页时 include('execute.php?cmd=test'); 或 对不起,我想你们不太明白我的意思。我想做的是,第2页有一些内容必须只在需要时显示,因此我在第2页创建了一个函数,这样我就可以调用它。必须通过呼叫在第1页查看的内容。因此,我
www.mypage.com/execute.php?cmd=test
输出是hello world
但是当将同一页包含到第1页时
include('execute.php?cmd=test');
或
对不起,我想你们不太明白我的意思。我想做的是,第2页有一些内容必须只在需要时显示,因此我在第2页创建了一个函数,这样我就可以调用它。必须通过呼叫在第1页查看的内容。因此,我尝试了执行“execute.php?cmd=test”正常工作的方法,但这次不行了,我想您应该这样做:
$test = $_GET['cmd'];
这将在两个页面中都起作用。我想您应该这样做:
$test = $_GET['cmd'];
这将在两个页面中都起作用。虽然我认为我理解您的意图,但这是错误的方法。使用两个页面都将包含的第三个PHP脚本。这样,您就不必破解
$\u GET
超级全局变量
包括/somescript.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
page1.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
page2.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
当然,文件includes/somescript.php
不应该这样执行,它应该包含一个函数或一个包含参数的类,然后从页面中调用。这取决于你,如果你要去OO或程序化
程序性
包括/somescript.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
page1.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
面向对象
包括/somescript.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
page2.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
虽然我想我理解你的意图,但这是错误的方法。使用两个页面都将包含的第三个PHP脚本。这样,您就不必破解$\u GET
超级全局变量
包括/somescript.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
page1.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
page2.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
当然,文件includes/somescript.php
不应该这样执行,它应该包含一个函数或一个包含参数的类,然后从页面中调用。这取决于你,如果你要去OO或程序化
程序性
包括/somescript.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
page1.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
面向对象
包括/somescript.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
page2.php
// do something with $cmd
if ($cmd == 'test') echo 'Hello world';
$cmd = 'test';
include 'includes/somescript.php';
$cmd = $_GET['cmd'];
include 'includes/somescript.php';
function execute($cmd) {
// ...
}
include `includes/somescript.php';
execute('test');
class CommandHandler {
public function execute($cmd) {
//...
}
}
include `includes/somescript.php';
$h = new CommandHandler();
$h->execute($_GET['cmd']);
第一种语法不会让您走得太远。第二个可以,但是您也可以直接设置一个变量并在execute.php中引用它,而不使用$\u GET public变量。第二个可以,但是您也可以直接设置一个变量,并在execute.php中引用它,而不使用$\u GET public变量。