PHP basic knowledge notes sharing

小云云
Release: 2023-03-20 14:32:01
Original
2871 people have browsed it

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>
Copy after login

Notes
  1. PHP statements end with a semicolon (;)

  2. 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;"><!DOCTYPE html><html><body><?php// 这是单行注释# 这也是单行注释/*<br/>这是多行注释块<br/>它横跨了<br/>多行<br/>*/?></body></html><br/></span>
Copy after login

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[&#39;y&#39;]=$GLOBALS[&#39;x&#39;]+$GLOBALS[&#39;y&#39;];<br/>} <br/><br/>myTest();echo $y; // 输出 15?><br/></span>
Copy after login

4. Output statements echo and print

Syntax
<span style="font-size: 14px;"><?php  <br/>$a=&#39;hello &#39;;$b=&#39;php world!&#39;;echo $a,$b,&#39;<br />&#39;;//echo 可以用逗号分隔字符串变量来显示  <br/>print $a.$b.&#39;<br />&#39;;//而print不能使用逗号,只能用点号分隔,    <br/>?><br/></span>
Copy after login
Difference
  1. echo command is the same as print command, there is no difference

  2. There is a difference between the echo function and the print function

  3. echo() has no return value. Same as the echo command

  4. print() has a return value, if successful, it returns 1, false, returns 0

5. Operator (only different from JS)

##$txt1 = "Hello" $txt2 = $txt1 . " world!"Now $txt2 contains "Hello world!" ##.=Compare##$x <> ; $y"Returns true if $x is not equal to $y. #and和or$x xor $y#&Returns true if both $x and $y are true if $x and $y If at least one is true, return true. If $x is not true, return true. ##$x <> $yReturns true if $x is not equal to $y. !==Not congruent$x ! == $yReturns true if $x is completely different from $y.

6.判断与循环

判断:
  • if…else…语句

  • switch语句

循环:
  • for循环

  • foreach循环

foreach循环示例
<span style="font-size: 14px;"><?php <br/>$colors = array("red","green","blue","yellow"); <br/>foreach ($colors as $value) {  <br/>echo "$value <br>";<br/>}<br/>?>//输出 red 、 green 、 blue 、 yellow<br/></span>
Copy after login

7.引用

PHP引用有两种方式:include 与 require

include 和 require 语句是相同的,除了错误处理方面:
  • require 会生成致命错误(E_COMPILE_ERROR)并停止脚本

  • include 只生成警告(E_WARNING),并且脚本会继续

语法:

<span style="font-size: 14px;">include &#39;filename&#39;<br/></span>
Copy after login

<span style="font-size: 14px;">require &#39;filename&#39;<br/></span>
Copy after login

8.超全局变量

  • $GLOBALS

  • $_SERVER

  • $_REQUEST

  • $_POST

  • $_GET

  • $_FILES

  • $_ENV

  • $_COOKIE

  • $_SESSION

$GLOBALS — 引用全局作用域中可用的全部变量
<span style="font-size: 14px;"><?php $x = 75; <br/>$y = 25;function addition() { <br/>  $GLOBALS[&#39;z&#39;] = $GLOBALS[&#39;x&#39;] + $GLOBALS[&#39;y&#39;]; <br/>}<br/><br/>addition(); <br/>echo $z; <br/>?><br/></span>
Copy after login
PHP $_SERVER

$_SERVER 这种超全局变量保存关于报头、路径和脚本位置的信息。

SymbolNameExampleExplanation
Concatenation


##.Concatenation
Concatenation assignment$txt1 = "Hello" $txt1 .= " world!"##Now $txt1 contains "Hello world! "



##<>
is not equal to Logic



$x and $y##Returns true if both $x and $y are true. ## or
$x or $yIf at least one of $x and $y is true, return true #xorXOR
##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 + $yUnion of $x and $y (but does not cover duplicate keys, the same key retains the first one)
== Equal$x == $yIf $x and $y have The same key/value pair, returns true.
===Congruent$x = == $yReturns true if $x and $y have the same key/value pairs in the same order and type.
!=Not equal$x != $yReturns true if $x is not equal to $y.
##<>Not equal
ElementDescription
##$_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[&#39;PHP_SELF&#39;];?>"><br/>Name: <input type="text" name="fname"><input type="submit"></form><?php $name = $_REQUEST[&#39;fname&#39;]; <br/>echo $name; <br/>?></body></html><br/></span>
Copy after login

三、数据类型

1.字符串函数

PHP strlen() 函数

strlen() 函数返回字符串的长度,以字符计。

<span style="font-size: 14px;"><?phpecho strlen("Hello world!");?>//结果输出12(多个连续的空格不会被看作同一个)<br/></span>
Copy after login
PHP strpos() 函数

strpos() 函数用于检索字符串内指定的字符或文本。
如果找到匹配,则会返回首个匹配的字符位置。如果未找到匹配,则将返回 FALSE。

<span style="font-size: 14px;"><?phpecho strpos("Hello world!","world");?>//返回 6<br/></span>
Copy after login

2.常量及设置常量

  • 常量是单个值的标识符(名称)。在脚本中无法改变该值。

  • 有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号)。

  • 与变量不同,常量贯穿整个脚本是自动全局的。

    设置常量函数 define()
  • 首个参数定义常量的名称

  • 第二个参数定义常量的值

  • 可选的第三个参数规定常量名是否对大小写不敏感。默认是 false。

<span style="font-size: 14px;"><?phpdefine("PAI", "3.14", true);echo pai;?>//创建一个对大小写不敏感的常量<br/></span>
Copy after login

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>
Copy after login
多维数组的创建
<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>
Copy after login

四、功能函数

1.日期函数

PHP Date() 函数

语法:date(format,timestamp)

  1. PHP Date() 函数把时间戳格式化为更易读的日期和时间。

  2. 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>
Copy after login
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

Summary of PHP basic knowledge, after learning these, you can directly advance to senior PHP programmer

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!