PHP study notes--about variables in php_PHP tutorial
Variable variables in PHP (please refer to the PHP manual for more information):
To put it bluntly, variable variables in php are to parse the value of a variable into a variable name and read the value of that variable name. Example:
<?<span php </span><span $a</span> = "China"; <span //</span><span 变量a</span> <span $b</span> = "a"; <span //</span><span 变量b</span> <span $China</span> = "I'm Chinese !"; <span //</span><span 变量China</span> <span $f</span> = "b"; <span //</span><span 变量f</span> <span echo</span> <span $a</span>."<br />"; <span //</span><span 输出 China</span> <span echo</span> $<span $a</span>."<br />"; <span //</span><span 输出 I'm Chinese --这里像要当做可变变量解析,必须在前面多加一个$符号</span> <span $a</span> = "f"; <span //</span><span 改变变量指向的名称(这里就是可变变量的应用)</span> <span echo</span> $<span $a</span>."<br />"; <span //</span><span 经过上面指向变量f后输出 b</span> <span $a</span> = "b"; <span //</span><span 同上</span> <span echo</span> $<span $a</span>."<br /><br />"; <span //</span><span 输出 a</span> <span echo</span> <span $b</span>."<br />"; <span //</span><span 输出 a</span> <span echo</span> $<span $b</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $$<span $b</span>."<br /><br />"; <span //</span><span 输出 a</span> <span echo</span> <span $f</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $<span $f</span>."<br />"; <span //</span><span 输出 a</span> <span echo</span> $$<span $f</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $$$<span $f</span>."<br /><br />"; <span //</span><span 输出 a</span> <span $</span><span $a</span> = "China"; <span //</span><span 前面最后一个已经更改了变量为b所谓$$a=$b 也就是改变的$b的值</span> <span echo</span> <span $b</span>."<br />"; <span //</span><span 输出 China</span> <span echo</span> $<span $b</span>; <span //</span><span 输出 I'm Chinese</span> ?>
Note: Mutable variables cannot be applied to $this and superglobal variables (the scope of PHP variables is different from other high-level programming languages. See the code)
<?<span php </span><span $name</span> = 'man'<span ; $</span><span $name</span> = 'abc'; <span //</span><span 如果事先没有man这个变量。就新建一个man变量。 然后把abc赋值过去</span> $$<span $name</span> = 'def'<span ; </span><span echo</span> <span $man</span>."<br />"; <span //</span><span 输出abc</span> <span echo</span> <span $abc</span>; <span //</span><span 输出def</span> <span echo</span> "<br /> <hr />"<span ; </span><span function</span><span show() { </span><span global</span> <span $name</span>; <span //</span><span 这里的global并不是设置为全局变量。而是引用</span> <span echo</span> <span $name</span>."<br />"; <span //</span><span 输出man</span> <span } </span><span function</span><span showtwo() { </span><span //</span><span global $name; //echo $name."<br />";</span> <span echo</span> <span $GLOBALS</span>['name']; <span //</span><span 超全局变量数组</span> <span } show(); showtwo(); </span>?>
Variable function:
<?<span php </span><span function</span><span b() { </span><span echo</span> "这是B"<span ; } </span><span function</span> c(<span $name</span> = "China") <span //</span><span 设默认值</span> <span { </span><span echo</span> "这是<span $name</span>"<span ; } </span><span $a</span> = 'b'<span ; </span><span $a</span>(); <span //</span><span 找值所在的函数</span> <span $a</span> = 'c'<span ; </span><span $a</span><span ();<br /> </span>?>
A typical application of variable variables:
<!DOCTYPE html <span PUBLIC</span> "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div> <form action="#" method="post"> <label>name: </label> <input type="text" name="name" /><br /> <label>pwd : </label> <input type="text" name="pwd" /><br /> <label>tag : </label> <input type="text" name="tag" /><br /> <input type="submit" value="提交" /> </form> </div> <?<span php </span><span foreach</span>(<span $_POST</span> <span as</span> <span $key</span>=><span $value</span><span ) { </span><span //</span><span print_r($_POST);</span> $<span $key</span> = <span $value</span><span ; } </span><span //</span><span extract($_POST); //从数组中将变量导入到当前的符号表 --自行查找php手册</span> <span echo</span> <span $name</span>."<br />"<span ; </span><span echo</span> <span $pwd</span>."<br />"<span ; </span><span echo</span> <span $tag</span>."<br />"<span ; </span>?> </body> </html>
Supplement:
Characteristics of variables. If a variable has not been declared in advance, then if you want to assign a value to a variable, one of the operations of PHP in the background is that when you assign a value to the undeclared variable, the background has already declared the variable for you. Just look at the example:
<?<span php </span><span class</span><span A { </span><span public</span> <span function</span><span show() { </span><span //</span><span 记住这里的name 实现是没有声明的。</span> <span echo</span> (<span isset</span>(<span $this</span>->name)?"true":"false")." -- "<span ; </span><span echo</span> <span $this</span>-><span name; } } </span><span $A</span> = <span new</span> A(); <span //</span><span 实例化 //直接输出,是没有任何结果的. 因为没有这个变量 。。这里可以用isset判断为false</span> <span $A</span>->show(); <span //</span><span 输出 "fase -- " //这里进行赋值,在赋值时,后台默认声明此变量</span> <span $A</span>->name = "我有输出了。这个变量被声明了!"<span ; </span><span echo</span> "<br />"<span ; </span><span $A</span>->show(); <span //</span><span 输出 "true -- 我有输出了。这个变量被声明了!"</span> ?>
Summary: After reading the above example, don’t be surprised if you see other people’s encapsulated code in the future and use it directly without declaring variables. That means you have to set it up yourself. You can just assign it directly. . . (PS: In fact, I was confused at first, because those who used to work on .NET would never allow this to happen in C#. I am used to using strong-type languages... When I look at this weak-type language, It’s true that you won’t get used to it very well at first)

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

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


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
