father.php如下定义:
复制代码 代码如下:
$jack = 1000;
?>
children.php 如下定义:
require("father.php");
$jack=123;
echo $jack."/n";
?>
php children.php
运行输出为123.
如果将$jack=123注释掉,运行为1000,如果将$jack=123放到require("father.php");之前,运行结果为1000.
比较好理解:php解释执行,解释到哪,执行到哪。。像$jack这种属于全局变量,如第一种情况的初始用它的时候是1000,是在require
的时候运行得到的,结果又被改成了123,所以运行结果输出123.
情况2:
children.php代码改为如下:
复制代码 代码如下:
require("father.php");
function testJack(){
if(!isset($jack)){
echo '$jack is null'."/n";
}
}//testJack
testJack();
?>
php children.php
运行结果为:$jack is null.也就是说在testJack()中引用的$jack是一个局部变量。
如果使用global关键字,声明这个$jack是一个全局变量,代码改为如下:
复制代码 代码如下:
require("father.php");
function testJack(){
global $jack;
if(!isset($jack)){
echo '$jack is null'."/n";
}else{
echo '$jack is not null'."/n";
}
}//testJack
testJack();
?>
则运行结果为$jack is not null!
情况3:
children.php代码如下:
复制代码 代码如下:
require("father.php");
class JackTest{
public function testJack(){
if(!isset($jack)){
echo '$jack is null'."/n";
}else{
echo '$jack is not null'."/n";
}
}//testJack
}
$jackTest = new JackTest();
$jackTest->testJack();
?>
运行结果输出:$jack is null
这是因为class中的这个函数的$jack这是一个局部变量啊。
如果在function testJack开头加 global $jack;那么就输出$jack is not null了。
比较容易理解。
情况4:
把文件名当做参数动态加载,代码如下:
复制代码 代码如下:
$casefile = $_SERVER['argv'][1];
echo $casefile."/n";
require($casefile);
echo $jack."/n";
?>
运行php children.php father.php
结果如下:
father.php
1000
也就是说我们动态加载程序运行成功了。。
情况5:
要把动态加载和类的定义结合起来:
目录关系式这样的:
|- c.php
|- Bfold - b.php
|- Afold - a.class.php (里面的函数引用了../Bfold/b.php )
也就是说 在c.php 中new 了class a.class ,而a.class.php 的一个函数中require 了Bfold 文件夹下的b.php ,这个require(../Bfold/b.php )报错,Warning: ……
因为你让服务器当前执行的是c.php 文件,所以php 解析的时候是把路径相对于c.php 而言的,你试试把(../Bfold/b.php )改成(Bfold/b.php )看看,应该就不会报错了。
下面是程序例子,说明在函数内部使用require_once (A.php ).
对require_once 的理解:
假设B.php 中引用了require_once(A.php); 这条语句。。
那么其实是相当于调用了A.php 这个匿名的lambda 函数去执行。如下图:
C.php 在一个函数调用中 require 了 B.php------》
B.php 在普通语句中 require 了 A.php--------》
A.php
现在我们调用 php B.php ;因为 B.php 在普通语句中使用了 require 调用了 A.php ,那么 A.php 会把它的相对 A 来说是全局变量的变量,注册到 B.php 的环境中。因为 B.php 是根开始调用文件,他的运行环境就是全局环境。所以A.php 文件中的变量在 B.php 可以被正常使用。
现在我们调用 php C.php ;那么 C 是在函数使用 require 调用了 B.php 的,然后 B 又调用了 A ,感觉在这个调用的过程中,相对 B 和 A 根运行环境是 C 的调用函数的环境 ,但 C 的调用函数如果要使用 B 和 A 中的变量,就没有办法了。
如果用 global $a, 去引用,那么由于 $a 在这种情况下不属于全局变量,引用不到。
如果用 $a 去引用,那么由于 $a 会被当成局部变量也引用不到的。

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
