1. Definition
##The PHP script is executed on the server and then sent to the browser Send back plain HTML results. This article mainly shares with you notes on the basic knowledge of PHP, hoping to help you.
2. Basic Grammar<span style="font-size: 14px;"><?phpecho "Hello World!";?><br/></span>
Notes
PHP statements end with a semicolon (;)
The last line of a PHP code block does not have to use a semicolon
1. Comments
PHP has three comment methods<span style="font-size: 14px;">nbsp;html><?php // 这是单行注释# 这也是单行注释/*<br/>这是多行注释块<br>它横跨了<br>多行<br>*/?><br></span>
2.Case sensitive
All user-defined functions, classes and keywords (such as if, else, echo, etc.) are case-insensitive
All variables are case-sensitive
3. Variables
1.PHP There is no command to create variables2. Variable naming rules
Variables start with the $ symbol, followed by the name of the variable
Variable names must begin with a letter or underscore
Variable names must not begin with a number
Variable names are case-sensitive ($y and $Y are two different variables)
3. PHP has three different variable scopes: local (local) global (global) static (static)
Variables declared outside functions have Global scope. Can only be accessed outside the function.
#Variables declared inside a function have LOCAL scope and can only be accessed inside the function.
Methods to access external variables inside a function<span style="font-size: 14px;">//使用 global 关键词<br/><?php<br/>$x=5;<br/>$y=10;<br/>function myTest() {<br/> global $x,$y; <br/> $y=$x+$y;<br/>}<br/><br/>myTest();<br/>echo $y; // 输出 15?>//PHP 同时在名为 $GLOBALS[index] 的数组中存储了所有的全局变量。<br/><?php<br/>$x=5;<br/>$y=10;<br/>function myTest() {<br/> $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];<br/>} <br/><br/>myTest();echo $y; // 输出 15?><br/></span>
4. Output statements echo and print
Syntax<span style="font-size: 14px;"><?php <br/>$a='hello ';$b='php world!';echo $a,$b,'<br />';//echo 可以用逗号分隔字符串变量来显示 <br/>print $a.$b.'<br />';//而print不能使用逗号,只能用点号分隔, <br/>?><br/></span>
Difference
echo command is the same as print command, there is no difference
There is a difference between the echo function and the print function
echo() has no return value. Same as the echo command
print() has a return value, if successful, it returns 1, false, returns 0
5. Operator (only different from JS)
Symbol | Name | Example | Explanation |
---|---|---|---|
Concatenation | |||
##. | Concatenation | ##$txt1 = "Hello" $txt2 = $txt1 . " world!"Now $txt2 contains "Hello world!" | |
Concatenation assignment | $txt1 = "Hello" $txt1 .= " world!" | ##Now $txt1 contains "Hello world! " | Compare |
|
## |
||
is not equal to | ##$x Returns true if $x is not equal to $y.Logic | ||
$x and $y | ##Returns true if both $x and $y are true. | or | ## or |
$x or $y | If at least one of $x and $y is true, return true | #xor | XOR |
##If there is and only one of $x and $y is true, returns true | #& | # and | ##. $x && $y |
#| | | or | $x || $y | |
! | 不 | !$x | |
Array operators | # # | ||
+ | United | $x + $y | Union of $x and $y (but does not cover duplicate keys, the same key retains the first one) |
== | Equal | $x == $y | If $x and $y have The same key/value pair, returns true. |
=== | Congruent | $x = == $y | Returns true if $x and $y have the same key/value pairs in the same order and type. |
!= | Not equal | $x != $y | Returns true if $x is not equal to $y. |
## | Not equal | ##$x $yReturns true if $x is not equal to $y. | |
Not congruent | $x ! == $y | Returns true if $x is completely different from $y. |
Element | Description |
---|---|
##$_SERVER['PHP_SELF'] | Returns the file name of the currently executing script. |
$_SERVER[‘GATEWAY_INTERFACE’] | Returns the version of the CGI specification used by the server. |
$_SERVER[‘SERVER_ADDR’] | Returns the IP address of the server where the script is currently running. |
$_SERVER['SERVER_NAME'] | Returns the host name of the server where the script is currently running (such as www .w3school.com.cn). |
$_SERVER['SERVER_SOFTWARE'] | Returns the server identification string (such as Apache/2.2.24) . |
$_SERVER['SERVER_PROTOCOL'] | Returns the name and version of the communication protocol when the page was requested (for example, "HTTP/1.0"). |
$_SERVER[‘REQUEST_METHOD’] | Returns the request method used to access the page (such as POST). |
$_SERVER[‘REQUEST_TIME’] | Returns the timestamp when the request started (for example, 1577687494). |
$_SERVER['QUERY_STRING'] | Returns the query string, if this is accessed through the query string page. |
$_SERVER[‘HTTP_ACCEPT’] | Returns the request headers from the current request. |
$_SERVER['HTTP_ACCEPT_CHARSET'] | Returns the Accept_Charset header from the current request (such as utf-8, ISO-8859-1) |
$_SERVER['HTTP_HOST'] | Returns the Host header from the current request . |
$_SERVER['HTTP_REFERER'] | Returns the full URL of the current page (not reliable because not all User agents are supported). |
$_SERVER[‘HTTPS’] | Whether to query the script through the secure HTTP protocol. |
$_SERVER[‘REMOTE_ADDR’] | Returns the IP address of the user browsing the current page. |
$_SERVER[‘REMOTE_HOST’] | Returns the host name of the user browsing the current page. |
$_SERVER['REMOTE_PORT'] | Returns the port number used to connect to the Web server on the user's machine . |
$_SERVER[‘SCRIPT_FILENAME’] | Returns the absolute path of the currently executing script. |
$_SERVER[‘SERVER_ADMIN’] | This value specifies the SERVER_ADMIN parameter in the Apache server configuration file. |
$_SERVER[‘SERVER_PORT’] | The port used by the Web server. The default value is "80". |
$_SERVER[‘SERVER_SIGNATURE’] | Returns the server version and virtual host name. |
$_SERVER['PATH_TRANSLATED'] | Basic of the file system (non-document root directory) where the current script is located path. |
$_SERVER[‘SCRIPT_NAME’] | Returns the path of the current script. |
$_SERVER[‘SCRIPT_URI’] | Returns the URI of the current page. |
PHP $_REQUEST
PHP $_REQUEST 用于收集 HTML 表单提交的数据。
<span style="font-size: 14px;"><html><body><form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"><br/>Name: <input type="text" name="fname"><input type="submit"></form><?php $name = $_REQUEST['fname']; <br/>echo $name; <br/>?></body></html><br/></span>
三、数据类型
1.字符串函数
PHP strlen() 函数
strlen() 函数返回字符串的长度,以字符计。
<span style="font-size: 14px;"><?phpecho strlen("Hello world!");?>//结果输出12(多个连续的空格不会被看作同一个)<br/></span>
PHP strpos() 函数
strpos() 函数用于检索字符串内指定的字符或文本。
如果找到匹配,则会返回首个匹配的字符位置。如果未找到匹配,则将返回 FALSE。
<span style="font-size: 14px;"><?phpecho strpos("Hello world!","world");?>//返回 6<br></span>
2.常量及设置常量
常量是单个值的标识符(名称)。在脚本中无法改变该值。
有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号)。
-
与变量不同,常量贯穿整个脚本是自动全局的。
设置常量函数 define()
首个参数定义常量的名称
第二个参数定义常量的值
可选的第三个参数规定常量名是否对大小写不敏感。默认是 false。
<span style="font-size: 14px;"><?phpdefine("PAI", "3.14", true);echo pai;?>//创建一个对大小写不敏感的常量<br/></span>
3.数组
PHP有三种数组形式:
索引数组 - 带有数字索引的数组
关联数组 - 带有指定键的数组
多维数组 - 包含一个或多个数组的数组
数组相关函数
array() 用于创建数组
count() 用于得出数组长度
sort() - 以升序对数组排序
rsort() - 以降序对数组排序
asort() - 根据值,以升序对关联数组进行排序
ksort() - 根据键,以升序对关联数组进行排序
arsort() - 根据值,以降序对关联数组进行排序
krsort() - 根据键,以降序对关联数组进行排序
关联数组的创建与循环
<span style="font-size: 14px;"><?php //关联数组使用foreach循环<br/>$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");<br/>foreach($age as $x=>$x_value) { <br/>echo "Key=" . $x . ", Value=" . $x_value; <br/>echo "<br>";<br/>}?><br/></span>
多维数组的创建
<span style="font-size: 14px;">$cars = array<br/> ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15)<br/> );<br/></span>
四、功能函数
1.日期函数
PHP Date() 函数
语法:date(format,timestamp)
PHP Date() 函数把时间戳格式化为更易读的日期和时间。
format格式:
d - 表示月里的某天(01-31)
m - 表示月(01-12)
Y - 表示年(四位数)
h - 带有首位零的 12 小时小时格式
i - 带有首位零的分钟
s - 带有首位零的秒(00 -59)
a - 小写的午前和午后(am 或 pm)
1 - 表示周里的某天
其他字符,比如 “/”, “.” 或 “-” 也可被插入字符中,以增加其他格式
<span style="font-size: 14px;"><?php//不传第二个参数,默认是目前的时间。echo "今天是 " . date("Y/m/d") ;?><br/></span>
PHP mktime()
mktime() 函数返回日期的 Unix 时间戳。Unix 时间戳包含 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)与指定时间之间的秒数。
语法:mktime(hour,minute,second,month,day,year)。
相关推荐:
Summary of basic knowledge of php (necessary for novices)
Mastering the basic knowledge of php - four kinds of delimiters
The above is the detailed content of PHP basic knowledge notes sharing. For more information, please follow other related articles on the PHP Chinese website!

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


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

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
