简体   繁体   English

php-include(file.php)是否与运行它相同?

[英]php - is include(file.php) the same as running it?

I have a cron job set up in my cron.php file. 我的cron.php文件中设置了cron作业。

I also have a function() placed in that same file, which I want to use in other places. 我还在同一个文件中放置了一个function() ,我想在其他地方使用它。

Now the cron.php file has been set to run only one every 24 hours, meaning I don't want it to run multiply times. 现在,将cron.php文件设置为每24小时仅运行一次,这意味着我不希望它运行多次。

somepage.php: somepage.php:

<?php
include_once("/cron.php");

echo someFunction(); //Function taken from cron.php

?>

Will the cron.php then run every time I load the somepage.php file? 请问cron.php然后运行每次我打开的时候somepage.php文件?

Yes it will. 是的,它会的。

When you want to cache the output from cron.php, you need to make an extra file, eg. 当您要缓存cron.php的输出时,需要制作一个额外的文件,例如。 'cron-output.txt', and make cron.php put it's info there. 'cron-output.txt',然后将cron.php放在此处。 Ultimately you could use a database too. 最终,您也可以使用数据库。

Yes, the code will run as if it exists within that file. 是的,代码将像在该文件中一样运行。 Unencapsulated PHP code will be run as if it were inserted directly in the place of the include directive, and functions will only be run as called. 未封装的PHP代码将像直接插入到include指令的位置一样运行,并且函数只能按调用的方式运行。

The prerequisite, however, is that the code must be wrapped in PHP open and close tags. 但是,前提条件是必须将代码包装在PHP的open和close标签中。 From the PHP docs on include() : include()PHP文档中

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. 包含文件后,解析将从目标文件的开头退出PHP模式并进入HTML模式,然后在结尾处再次恢复。 For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags. 因此,目标文件中应作为PHP代码执行的任何代码都必须包含在有效的PHP开始标记和结束标记内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM