Home >Backend Development >PHP Tutorial >PHP basic knowledge notes sharing
1. Definition
<span style="font-size: 14px;"><?phpecho "Hello World!";?><br/></span>
PHP statements end with a semicolon (;)
The last line of a PHP code block does not have to use a semicolon
<span style="font-size: 14px;">76c82f278ac045591c9159d381de2c57100db36a723c770d327fc0aef2ce13b16c04bd5ca3fcae76e30b72ad730ca86d095a8525499549fa892a87d025984cb436cc49f0c466276486e50c850b7e495673a6ac4ed44ffec12cee46588e518a5e<br></span>
All user-defined functions, classes and keywords (such as if, else, echo, etc.) are case-insensitive
All variables are case-sensitive
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.
<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>
<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>
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
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 |
|
##a8093152e673feb7aba1828c43532094 |
||
is not equal to | ##$x <> ; $y"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. |
##a8093152e673feb7aba1828c43532094 | Not equal | ##$x a8093152e673feb7aba1828c43532094 $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 用于收集 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>
strlen() 函数返回字符串的长度,以字符计。
<span style="font-size: 14px;"><?phpecho strlen("Hello world!");?>//结果输出12(多个连续的空格不会被看作同一个)<br/></span>
strpos() 函数用于检索字符串内指定的字符或文本。
如果找到匹配,则会返回首个匹配的字符位置。如果未找到匹配,则将返回 FALSE。
<span style="font-size: 14px;">44ea3bf4a60d9637fbf709781f77d316//返回 6<br></span>
常量是单个值的标识符(名称)。在脚本中无法改变该值。
有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号)。
与变量不同,常量贯穿整个脚本是自动全局的。
首个参数定义常量的名称
第二个参数定义常量的值
可选的第三个参数规定常量名是否对大小写不敏感。默认是 false。
<span style="font-size: 14px;"><?phpdefine("PAI", "3.14", true);echo pai;?>//创建一个对大小写不敏感的常量<br/></span>
索引数组 - 带有数字索引的数组
关联数组 - 带有指定键的数组
多维数组 - 包含一个或多个数组的数组
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>
语法: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>
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!