How to implement include_once in php
include_once statement includes and runs the specified file during script execution. This behavior is similar to the include statement. The only difference is that if the file has already been included, it will not be included again. As the name of this statement implies, it will only be included once.
include_once can be used when the same file may be included more than once during script execution, and you want to ensure that it is only included once to avoid function duplication. Definition, variable reassignment and other issues.
Note:(Recommended learning: PHP Programming from Beginner to Master)
In PHP 4, the behavior of _once is not case-sensitive The letters are different in operating systems (such as Windows), for example:
include_once in PHP 4 running on case-insensitive operating systems
<?php include_once "a.php"; // 这将包含 a.php include_once "A.php"; // 这将再次包含 a.php!(仅 PHP 4) ?>
This behavior has been changed in PHP 5 , for example, in Windows the path is normalized first, so C:\PROGRA~1\A.php and C:\Program Files\a.php have the same implementation, and the file will only be included once.
include and include_once:
The files loaded by include will not be judged as duplicates. As long as there is an include statement, it will be loaded once (even if duplicate loading may occur) ).
When include_once loads a file, there will be an internal judgment mechanism to determine whether the previous code has been loaded.
It should be noted here that include_once is judged based on whether a file with the same path has been introduced before, rather than based on the content of the file (that is, the content of the two files to be introduced is the same, should you use include_once or not? Two will be introduced).
//test1.php <?php include './test2.php'; echo 'this is test1'; include './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1this is test2 //test1.php <?php include './test2.php'; echo 'this is test1'; include_once './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1 //test1.php <?php include_once './test2.php'; echo 'this is test1'; include './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1this is test2 //test1.php <?php include_once './test2.php'; echo 'this is test1'; include_once './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1
The above is the detailed content of How to implement include_once in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

In this chapter, we are going to learn the following topics related to routing ?

Validator can be created by adding the following two lines in the controller.
