1. There are two ways to reference files: require and include
The use of require is such as require("MyRequireFile.php");
. This function is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.
The usage method of include is as follows: include("MyIncludeFile.php");
. This function is generally placed in the processing part of flow control. The PHP program webpage only reads the include file when it reads it. In this way, the process of program execution can be simplified.
2. Comments
<span>php </span><span>echo</span> "这是第一种例子。\n"; <span>//</span><span> 本例是 C++ 语法的注释</span><span>/*</span><span> 本例采用多行的 注释方式 </span><span>*/</span><span>echo</span> "这是第两种例子。\n"<span>; </span><span>echo</span> "这是第三种例子。\n"; <span>#</span><span> 本例使用 UNIX Shell 语法注释</span> ?>
Comments: The information explained is what and why.
3. Constant type
PHP defines the following constants in constants.
__FILE__
This default constant is the PHP program file name. If a file is referenced (include or require), the constant in the referenced file is the referenced file name, not the file name that refers to it.
__LINE__
This default constant is the number of PHP program lines. If a file is referenced (include or require), the constant in the referenced file is the line that refers to the file, not the file line that references it.
PHP_VERSION
This built-in constant is the version of the PHP program, such as '3.0.8-dev'.
PHP_OS
This built-in constant refers to the name of the operating system that executes the PHP parser, such as 'Linux'.
TRUE
This constant is the truth value (true).
FALSE
This constant is the false value (false).
E_ERROR
This constant points to the most recent error.
E_WARNING
This constant points to the nearest warning.
E_PARSE
This routine is a potential problem in parsing grammar.
E_NOTICE
This routine means something unusual occurs but it is not necessarily an error. For example, access a variable that does not exist.
For these constants starting with E_, you can refer to the error_reporting() function for more related instructions.
Of course, when writing programs, the above default constants are not enough. The define() function allows us to define the constants we need. See the example below
<span>php </span><span>define</span>("COPYRIGHT", "Copyright © 2000, netleader.126.com"<span>); </span><span>echo</span><span> COPYRIGHT; </span><span>echo</span><span>__FILE__</span><span>; </span>?>
4. Declare variables (case sensitive)
<span>php </span><span>/*</span><span>* * @file variable.php * @author suguolong * @date 2015/07/29 16:49:08 * @brief * *</span><span>*/</span><span>/*</span><span> 定义字符串变量 </span><span>*/</span><span>$mystring</span> = "我是字符串"<span>; </span><span>$WilsonPeng</span> = "真是认真的作者"<span>; </span><span>$NewLine</span> = "换行了\n"<span>; </span><span>/*</span><span> 定义整型变量 </span><span>*/</span><span>$int1</span> = 38<span>; </span><span>$int2</span> = 49<span>; </span><span>$hexint</span> = 0x10<span>; </span><span>/*</span><span> 定义浮点变量 </span><span>*/</span><span>$float1</span> = 1.732<span>; </span><span>$float2</span> = 1.4E+2<span>; </span><span>/*</span><span> 定义数组变量 </span><span>*/</span><span>$MyArray1</span> = <span>array</span>("子", "丑", "寅", "卯"<span>); </span><span>$MyArray2</span> = <span>array</span><span>( </span>"地支" => <span>array</span>("子", "丑", "寅", "卯"), "生肖" => <span>array</span>("鼠", "牛", "虎", "兔"), "数字" => <span>array</span>(1, 2, 3, 4<span>) ); </span><span>/*</span><span> 类的定义 </span><span>*/</span><span>class</span><span> foo { </span><span>function</span><span> do_foo () { </span><span>echo</span> "Doing foo.\n"<span>; } } </span><span>/*</span><span> 类的使用 </span><span>*/</span><span>$bar</span> = <span>new</span><span> foo; </span><span>$bar</span> -><span> do_foo (); </span><span>$bar</span> -><span> do_foo (); </span><span>$bar</span> -><span> do_foo (); </span><span>/*</span><span> 定义布尔值 </span><span>*/</span><span>$booleanval_true</span> = <span>true</span><span>; </span><span>$booleanval_false</span> = <span>false</span><span>; </span><span>/*</span><span> 使用变量 </span><span>*/</span><span>echo</span> "boolean value of true: \n"<span>; </span><span>echo</span><span>$booleanval_true</span><span>; </span><span>echo</span> "\n"<span>; </span><span>echo</span> "boolean value of false: \n"<span>; </span><span>echo</span><span>$booleanval_false</span><span>; </span><span>echo</span> "\n"<span>; </span><span>/*</span><span> vim: set expandtab ts=4 sw=4 sts=4 tw=100: </span><span>*/</span>?>
[suguolong@cp01-rdqa-dev004.cp01.baidu.com sugl]$ php variable.php Doing foo. Doing foo. Doing foo. boolean value of true: 1 boolean value of false: [suguolong@cp01-rdqa-dev004.cp01.baidu.com sugl]$
5. Use of variables
When the PHP program is executed, the system will retain a global variable in the memory Area. In actual use, you can retrieve the required variables through $GLOBALS["Variable Name"].
The $GLOBALS array is a special variable in the PHP program. It does not need to be defined. The system will automatically match related variables in it. In the function, you don’t need to worry about whether the $GLOBALS array has been globally defined, you can use it directly.
Similar to the $GLOBALS variable, there is the $php_errormsg string variable. If the track_errors option in the PHP configuration file (php.ini/php3.ini) is turned on, there will be a global variable $php_errormsg where you can see the error information.
In PHP, the effective scope of global variables is limited to the main program and will not affect variables with the same name in functions, that is, global variables and local variables do not infringe each other. If you want variables to penetrate into functions, you need to use the $GLOBALS array or use global definitions.
As for the information entered by the user in FORM, how to process it? It would be nice if in the PHP configuration file, when track_vars is set to On, the variable name is used directly. As shown in the following example, when next.php is executed, the system will automatically generate two variables $username and $sex, which can be used directly. Compared with traditional CGI, which has to be parsed by itself, PHP is really amazing.
The above introduces the PHP learning notes (1), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

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.

Dreamweaver Mac version
Visual web development tools
