现在自学PHP 看的是兄弟连的视频教程 学到变量范围的时候 视频老师的例子是 :
$a = 10;
function demo(){
$a+=10;
}
demo();
可是我在照做例子的时候出现
Notice: Undefined variable: a 这样的错误提示,在函数里先给$a赋值就没问题了。我的是5.3+的。是不是php版本的问题?
回复讨论(解决方案)
这个程序中在函数中 $a是一个局部变量,如果你想引用函数外你预先声明的变量$a你必须要加入global
$a=10;
function demo()
{
global $a;
$a+=5;
echo $a;
}
demo();
echo $a;
?>
所有的版本都是这样
这个程序中在函数中 $a是一个局部变量,如果你想引用函数外你预先声明的变量$a你必须要加入global
$a=10;
function demo()
{
global $a;
$a+=5;
echo $a;
}
demo();
echo $a;
?>
不错!~
我知道全局变量要加GLOBAL。可是视频教程的例子没有加GLOBAL 一样可以执行,没错误提示。我在本地照做就提示 Notice: Undefined variable: a这个错误,不知道是怎么回事
我知道全局变量要加GLOBAL。可是视频教程的例子没有加GLOBAL 一样可以执行,没错误提示。我在本地照做就提示 Notice: Undefined variable: a这个错误,不知道是怎么回事
通过自己尝试老师的代码,让你意识到,变量,全局变量,局部变量的作用域的重要性,远比弄清楚老师用的哪个版本实现的这个代码,跟重要
因为他把notice错误级别屏蔽了。
你在首行加上:error_reporting(E_ALL & ~E_NOTICE); 也不会报错。
我明白你的意思,但是这个问题还是困惑着我,只想弄清楚是不是PHP版本的问题,才导致这个错误,下次会注意这个问题。到现在也没有人直接回答我这个问题!!!
与PHP版本无关,原因我在#6说明了。
明白了,谢谢版主的回答,试了一下还真是这样。

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


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

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

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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),
