2013-05-06 177 views
-3

我正在写一个php脚本,在4个不同的移动平台上发送推送通知。每个平台都需要自己的设置来发送推送通知,这意味着4种不同的PHP脚本。如何通过我的PHP脚本运行一个PHP脚本?

我可以编写一个巨大的PHP脚本,里面包含所有4个脚本,并且使用if-ifelse语句完成工作。

但是我不觉得这个解决方案整齐在所有...我已经看到,在可以包括内部另一个像一个PHP脚本:

include 'testing.php'; 

但是如何我现在跑的?我想从当前脚本执行这个脚本,当完成时,继续执行我的脚本。可能吗?

+1

回应来自该文件的'testing.php'内的任何内容。 – samayo 2013-05-06 16:17:42

+0

你是什么意思?如果我想通过传递参数来调用函数? – donparalias 2013-05-06 16:20:25

+0

'include()'运行一个包含的php,然后父php继续 – 2013-05-06 16:22:04

回答

2

将PHP文件包含在另一个中意味着它正在该包装被写入的那一行被调用和执行。

<? 
do something... //does some php stuff 

include("another_file.php"); /* here the code of another_file.php gets "included" 
and any operations that you have coded in that file gets executed*/ 

do something else.. //continues doing rest of the php stuff 
?> 

要回答你的问题的意见,假设another_file.php有一个函数:

<? 
function hi($name) 
{ 
    echo "hi $name"; 
} 
?> 

可以包括文件,并调用该函数在父文件:

parent.php:

<? 
include("another_file.php"); 
hi("Me"); 
?> 
+0

如果我想运行一个PHP脚本中的函数?如果我想从该文件传递参数到该文件?可能吗? – donparalias 2013-05-06 16:23:50

+0

是的,如果你已经包含了定义该函数的文件,那么你可以在这个文件中调用该函数 – raidenace 2013-05-06 16:25:09

+0

问题是我需要从当前的php文件传递一个参数到该php文件。那可能吗?原因在当前文件我有“令牌”,我需要传递给另一个文件的“发送”功能 – donparalias 2013-05-06 16:26:34

1

你只需要将它包括在中间......就像那样简单。我会以一个例子向你展示。

<?php 

echo "It's a nice day to send an email OR an sms.<br>"; 
$Platform = "mobile"; 

if ($Platform == "mobile") 
    { 
    include 'testing.php'; 
    } 
else 
    { 
    include 'whatever.php'; 
    } 

echo "The message was sent! Now I will print from 0 to 100:<br>"; 
for ($i = 0; $i<= 100; $i++) 
    echo $i . '<br>'; 
?> 

Althought,如果有超过1个平台如你所说,你可能想学习使用PHP switch statment

为了更好的理解和我学会了:

当您使用include,你literately把包含文件的代码在你的代码*。说 'testing.php' 具有确实echo "Hello world";回波,则上述是相同的,因为这:

testing.php

<?php 
echo "Hello world"; 
?> 

的index.php(或任何名称):

<?php 

echo "It's a nice day to send an email OR an sms.<br>"; 
$Platform = "mobile"; 

if ($Platform == "mobile") 
    { 
    echo "Hello world"; 
    } 
else 
    { 
    include 'whatever.php'; 
    } 

echo "The message was sent! Now I will print from 0 to 100:<br>"; 
for ($i = 0; $i<= 100; $i++) 
    echo $i . '<br>'; 
?> 

*有几个例外:您需要将PHP标签放入包含文件<?php?>中,并且可以将多行代码作为一个代理(您不需要include中的大括号)。

+0

我如何将当前脚本的参数传递给脚本?那可能吗?导致在其他php文件中运行的函数需要参数运行。 – donparalias 2013-05-06 16:27:41

+0

你能否展示其他功能,以便我们更好地理解它?当前文件中可用的所有变量也可以在包含的文件中使用,因此您只需要使用该变量调用该函数即可。 – 2013-05-06 16:31:32