Table of Contents
" >##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.
<?phpecho "Hello World!";?>
Copy after login
" >2. Basic Grammar
<?phpecho "Hello World!";?>
Copy after login
" >Notes
" >1. Comments
<!DOCTYPE html><html><body><?php// 这是单行注释# 这也是单行注释/*
这是多行注释块
它横跨了
多行
*/?></body></html>
Copy after login
" >PHP has three comment methods
<!DOCTYPE html><html><body><?php// 这是单行注释# 这也是单行注释/*
这是多行注释块
它横跨了
多行
*/?></body></html>
Copy after login
" >2.Case sensitive
" >3. Variables
//使用 global 关键词
<?php
$x=5;
$y=10;
function myTest() {
  global $x,$y;  
  $y=$x+$y;
}

myTest();
echo $y; // 输出 15?>//PHP 同时在名为 $GLOBALS[index] 的数组中存储了所有的全局变量。
<?php
$x=5;
$y=10;
function myTest() {
  $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];


myTest();echo $y; // 输出 15?>
Copy after login
" >Methods to access external variables inside a function
//使用 global 关键词
<?php
$x=5;
$y=10;
function myTest() {
  global $x,$y;  
  $y=$x+$y;
}

myTest();
echo $y; // 输出 15?>//PHP 同时在名为 $GLOBALS[index] 的数组中存储了所有的全局变量。
<?php
$x=5;
$y=10;
function myTest() {
  $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];


myTest();echo $y; // 输出 15?>
Copy after login
" >4. Output statements echo and print
<?php  
$a='hello ';$b='php world!';echo $a,$b,'<br />';//echo 可以用逗号分隔字符串变量来显示  
print $a.$b.'<br />';//而print不能使用逗号,只能用点号分隔,    
?>
Copy after login
" >Syntax
<?php  
$a='hello ';$b='php world!';echo $a,$b,'<br />';//echo 可以用逗号分隔字符串变量来显示  
print $a.$b.'<br />';//而print不能使用逗号,只能用点号分隔,    
?>
Copy after login
" >Difference
5. Operator (only different from JS) " >5. Operator (only different from JS)
6.判断与循环" >6.判断与循环
判断:" >判断:
循环:" >循环:
foreach循环示例" >foreach循环示例
7.引用" >7.引用
include 和 require 语句是相同的,除了错误处理方面:" >include 和 require 语句是相同的,除了错误处理方面:
8.超全局变量" >8.超全局变量
$GLOBALS — 引用全局作用域中可用的全部变量" >$GLOBALS — 引用全局作用域中可用的全部变量
PHP $_SERVER" >PHP $_SERVER
PHP $_REQUEST" >PHP $_REQUEST
三、数据类型" >三、数据类型
1.字符串函数" >1.字符串函数
PHP strlen() 函数" >PHP strlen() 函数
PHP strpos() 函数" >PHP strpos() 函数
2.常量及设置常量" >2.常量及设置常量
设置常量函数 define()" >设置常量函数 define()
3.数组" >3.数组
PHP有三种数组形式:" >PHP有三种数组形式:
数组相关函数" >数组相关函数
关联数组的创建与循环" >关联数组的创建与循环
多维数组的创建" >多维数组的创建
四、功能函数" >四、功能函数
1.日期函数" >1.日期函数
PHP Date() 函数" >PHP Date() 函数
PHP mktime()" >PHP mktime()
Home Backend Development PHP Tutorial PHP basic knowledge notes sharing

PHP basic knowledge notes sharing

May 30, 2018 pm 02:28 PM
php basic knowledge notes

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles