以下文章提供了有关 PHP unset() 的概述。 unset() 方法的主要操作是销毁指定为其输入参数的变量。换句话说,它对所选变量执行重置操作。但是,其行为可能会根据要销毁的变量的类型而有所不同。 PHP4以上版本支持此功能。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
PHP unset() 语法
unset(mixed $selectedvar, mixed $selectedvar1,….., mixed $selectedvarN): void
- selectedvar: unset() 方法的强制参数。至少需要一个要取消设置的变量作为该方法的输入参数。
- selectedvarN: 可选参数,可以作为输入参数提供给 unset() 方法来重置它。
unset() 的用例
以下是不同的情况:
1.对局部变量应用 unset()
当局部变量传递给 unset 函数时,该函数会重置该变量。
示例:
代码:
<?php $input = "I have value!!!"; echo "The value of 'input' before unset: " . $input . "<br>"; unset($input); //Applying unset() method on $input variable echo "The value of 'input' after unset: " . $input; ?>
输出:
变量“input”中包含的值在执行 unset() 方法时被销毁。
2.对全局变量函数内的变量应用 unset
当用户尝试对函数内的变量使用 Unset 且该变量也定义为全局变量时,则 unset() 仅重置本地变量。全球范围不受影响。
示例:
代码:
<?php function Afunction() { $Avariable = 'local value'; echo "Within the function scope before unset: ".$Avariable."<br>"; global $Avariable; unset($Avariable); //Deletes the local ‘Avariable’ echo "Within the function scope after unset: ".$Avariable."<br>"; } $Avariable = 'Global Value'; //The global ‘Avariable’ echo "Out of the function scope before unset: ".$Avariable."<br>"; Afunction(); echo "Out of the function scope after unset: ".$Avariable."<br>"; ?>
输出:
变量“Avariable”的本地版本被破坏,而全局版本保持不变。
3.对函数内的全局变量应用 unset
如果函数内的变量也声明为全局变量,而用户需要销毁全局变量,则可以使用数组[$GLOBAL]来实现。
示例:
代码:
<?php function Afunction() { $Avariable = 'local value'; echo "Within the function scope before unset: ".$Avariable."<br>"; global $Avariable; unset($GLOBALS['Avariable']); //Resets the global ‘Avariable’ echo "Within the function scope after unset: ".$Avariable."<br>"; } $Avariable = 'Global Value'; echo "Out of the function scope before unset: ".$Avariable."<br>"; Afunction(); echo "Out of the function scope after unset: ".$Avariable."<br>"; ?>
输出:
变量“Avariable”的本地版本不受执行 unset 函数的影响,而变量的全局版本则设置为 null 值。
4.应用 unset() 来传递引用变量
如果对作为引用传递给函数的变量调用 unset(),则 unset() 仅重置本地变量。调用环境中的变量实例保持原样。
示例:
代码:
<?php function Afunction(&$Avariable) //’Avariable’ is the pass by reference { $Avariable = 'Internal value'; echo "Within the function scope before unset: ".$Avariable."<br>"; unset($Avariable); //Resets the local ‘Avariable’ echo "Within the function scope after unset: ".$Avariable."<br>"; } $Avariable = 'External Value'; echo "Out of the function scope before unset: ".$Avariable."<br>"; Afunction($Avariable); echo "Out of the function scope after unset: ".$Avariable."<br>"; ?>
输出:
通过引用变量“Avariable”传递时调用的 unset() 方法仅重置本地作用域中变量的内容,而不影响外部作用域的内容。
5.对静态变量应用 unset()
当静态变量设置为 unset() 方法的输入参数时,在调用函数 unset() 后,该变量将针对函数作用域中的剩余命令重置。
示例:
代码:
<?php function UnsetStatic() { static $staticvar; $staticvar++; echo "Before unset() method is called: $staticvar"."<br>"; //Deletes ‘staticvar’ only for the below commands within the scope of this ‘UnsetStatic’ function unset($staticvar); echo "after unset() method is called: $staticvar"."<br>"; } UnsetStatic(); UnsetStatic(); UnsetStatic(); ?>
输出:
变量“staticvar”仅在调用 unset() 方法后执行的命令中重置。
6.对数组元素应用 unset()
对数组元素应用 unset() 方法会从数组中删除该元素,而不执行重新索引操作。
示例:
代码:
<?php $arrayinput = [0 => "first", 1 => "second", 2 => "third"]; Echo "The array elements, before unset:"."<br>"; Echo $arrayinput[0]." ". $arrayinput[1]." ". $arrayinput[2]." "."<br>"; //Unset operation is called on the second element of the array ‘arrayinput’ unset($arrayinput[1]); Echo "The array elements, after unset:"."<br>"; Echo $arrayinput[0]." ". $arrayinput[1]." ". $arrayinput[2]." "; ?>
输出:
7.一次对多个元素应用 unset()
unset()方法支持一次删除多个变量。
示例:
代码:
<?php $input1 = "I am value1"; $input2 = "I am value2"; $input3 = "I am value3"; echo "The value of 'input1' before unset: " . $input1 . "<br>"; echo "The value of 'input2' before unset: " . $input2 . "<br>"; echo "The value of 'input3' before unset: " . $input3 . "<br>"; echo "<br>"; //Reseting input1, input2 and input3 together in single command unset($input1,$input2,$input3); echo "The value of 'input1' after unset: " . $input1."<br>"; echo "The value of 'input2' after unset: " . $input2."<br>"; echo "The value of 'input3' after unset: " . $input3."<br>"; ?>
输出:
以上是PHP 取消设置()的详细内容。更多信息请关注PHP中文网其他相关文章!

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

phpisusedforsenderemailsduetoitsbuilt-inmail()函数andsupportiveLibrariesLikePhpMailerandSwiftMailer.1)usethemail()functionforbasicemails,butithasimails.2)butithasimimitations.2)

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显着提升PHP应用的性能。

依赖性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增强量产生性,可验证性和Maintainability.itallowspasspassingDepentenciesLikEdenceSeconnectionSeconnectionStoclasseconnectionStoclasseSasasasasareTers,interitationApertatingAeseritatingEaseTestingEasingEaseTeStingEasingAndScalability。

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver Mac版
视觉化网页开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境