简体   繁体   中英

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

I have a cron job set up in my cron.php file.

I also have a function() placed in that same file, which I want to use in other places.

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.

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?

Yes it will.

When you want to cache the output from cron.php, you need to make an extra file, eg. 'cron-output.txt', and make cron.php put it's info there. 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.

The prerequisite, however, is that the code must be wrapped in PHP open and close tags. From the PHP docs on include() :

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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