search
HomeBackend DevelopmentPHP TutorialPHP syntax summary basics and variables_php skills

Recently there was a requirement for an H5 project, which required a server. After investigation, I decided to use PHP to implement an HTTP server, so I started to review the PHP syntax and record the key points so that I can review it at any time. The content is excerpted from w3school's PHP manual and reorganized according to my own understanding.

What is PHP?

PHP is an acronym for "PHP Hypertext Preprocessor"
The PHP code is executed on the server and the results are returned to the browser as plain text
PHP files can contain text, HTML, CSS and PHP code
The suffix of PHP files is ".php"
PHP scripts can be placed anywhere in the document.
PHP files usually contain HTML tags as well as some PHP script code.

PHP basic syntax

The script ends with
Statement ends with semicolon (;)
The closing tag of a code block also automatically indicates a semicolon (so you don't have to use a semicolon on the last line of a PHP code block).
// or # represents a single line comment
/**/ is a multi-line comment
Variables are case sensitive
User-defined functions, classes, and keywords that are case-insensitive (such as if, else, echo, etc.) are all

PHP Constants

Once a constant is defined, it cannot be changed or undefined
Constants are automatically global throughout the entire script
Setting a constant uses the define() function, which takes three parameters:

The first parameter defines the constant name
The second parameter defines the constant value
(Optional) The third parameter specifies whether the constant name is case-sensitive. The default is false.

<&#63;php
  define("GREETING", "Welcome!");
  echo GREETING;  //大小写敏感的常量

  define("Hello", "Welcome!", true);
  echo hello;   //大小写不敏感的常量
&#63;>

Valid constant names are opened with characters or underscores

PHP Variables

Variable weak type
Variables start with a $ symbol, followed by the name of the variable, such as $x=5;
Variables declared outside a function have Global scope and can only be accessed outside the function.
Variables declared inside a function have LOCAL scope and can only be accessed inside the function.
The global keyword is used to access global variables within a function. To do this, use the global keyword in front of the variable (inside the function):
Example 1:

<&#63;php
 $x=5;           // 全局作用域
 function myTest() {
  $y=10;         // 局部作用域
  echo "变量 x 是:$x";  // 不输出
  echo "变量 y 是:$x";  // 输出
 } 

 myTest();

 echo "变量 x 是:$x";   // 输出
 echo "变量 y 是:$x";   // 不输出
&#63;>

Example 2:

<&#63;php
 $x=5;
 $y=10;

 function myTest() {
  global $x,$y;
  $y=$x+$y;
 }

 myTest();
 echo $y;         // 输出 15
&#63;>

PHP Static keywords

Normally, all variables are deleted when the function completes/executes. However, sometimes I need to not delete a local variable. Achieving this will require further work.
To accomplish this, use the static keyword when you first declare the variable:

<&#63;php
 function myTest() {
  static $x=0;
  echo $x;
  $x++;
 }

 myTest();  // 输出0
 myTest();  // 输出1
 myTest();  // 输出2
&#63;>

Then, whenever the function is called, the information stored in this variable is the information contained when the function was last called.
Note: This variable is still local to the function.

Statement
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
PHP初学者必知的5个基本概念PHP初学者必知的5个基本概念Jun 21, 2023 am 10:24 AM

随着Internet技术的不断发展,PHP作为一门Web编程语言,越来越受到大家的喜爱。PHP的使用非常广泛,从简单的静态网站到大型的电子商务网站都可以使用PHP进行开发。无论是刚开始学习PHP的新手还是已经有一定经验的开发者,掌握一些基本概念都是非常必要的。在本文中,我将介绍PHP初学者必知的5个基本概念。变量PHP的变量是用来存储数据的容器。在PHP中,

如何解决PHP中的常见问题:语法错误和警告如何解决PHP中的常见问题:语法错误和警告Jun 11, 2023 pm 04:13 PM

PHP是一种广泛使用的服务器端编程语言,常用于构建动态网页。然而,在编写PHP脚本时,常常会遇到各种语法错误和警告。这些错误和警告可能导致代码无法正常运行或者出现问题,因此解决这些问题是非常重要的。本文将介绍PHP中的常见问题:语法错误和警告,并提供如何解决这些问题的有效方法。语法错误当在PHP中编写脚本时,语法错误是非常常见的问题。语法错误可能是由以下原因

PHP入门指南:PHP基础语法PHP入门指南:PHP基础语法May 20, 2023 am 08:39 AM

PHP是一种服务器端脚本语言,它被用来开发动态网站、Web应用程序和网页程序。PHP的应用范围非常广泛,无论是初学者还是有经验的开发人员,都可以从中获益。本文将为您提供PHP的基础语法入门指南。如果您想学习PHP编程,并且从头开始打好基础,那么您来对了地方。PHP的基本结构一个PHP程序包含以下三个部分:&lt;?php//PHP代码?&gt;代码两边的&

如何切实提高PHP编程的水平?如何切实提高PHP编程的水平?Jun 12, 2023 am 08:57 AM

PHP作为一种在Web开发中广泛应用的语言,它的普及程度和使用率相当高。许多初学者在学习PHP编程时都会遇到一些困难,比如不知道如何提高编程的水平。下面我们将介绍一些方法,让你更容易地提高自己的PHP编程水平。学习最新技术互联网技术的更新速度非常快,PHP也不例外。如果想要成为一名优秀的PHP程序员,首先要学习最新的PHP技术,掌握最新的Web开发技术,如M

PHP入门到高级 -- 学习基础语法和概念PHP入门到高级 -- 学习基础语法和概念Sep 09, 2023 am 10:01 AM

PHP入门到高级--学习基础语法和概念引言:PHP(HypertextPreprocessor)是一种流行的服务器端脚本语言,广泛应用于Web开发。在本文中,我们将从入门级别开始,逐步深入学习PHP的基础语法和概念,并提供代码示例供大家参考。一、PHP的安装与配置在开始学习PHP之前,首先需要在你的机器上安装PHP。你可以从官方网站(https://w

深入剖析PHP箭头符号的功能与特点深入剖析PHP箭头符号的功能与特点Mar 22, 2024 am 09:54 AM

PHP中的箭头符号(->)是一个非常重要的操作符,用于访问对象的属性和方法。本文将深入剖析PHP箭头符号的功能与特点,并提供具体的代码示例来帮助读者更好地理解其用法。在PHP中,箭头符号(->)主要用于访问对象的属性和方法。当我们创建一个对象时,可以通过箭头符号来访问该对象的属性或调用对象的方法。首先,让我们来看一个简单的示例,创建一个类并实例化

PHP开发经验分享:构建高效功能的技巧与建议PHP开发经验分享:构建高效功能的技巧与建议Nov 22, 2023 am 10:45 AM

PHP是一种广泛使用的服务器端脚本语言,用于开发动态网页和Web应用程序。许多网站和应用程序都使用PHP作为其后端开发语言,因此,掌握PHP开发技巧和经验对于构建高效功能至关重要。在本文中,我将分享一些我在PHP开发中学到的经验和技巧,旨在帮助开发人员提高他们的编码能力和构建高效功能的能力。精选适当的开发工具和框架在开始PHP开发之前,选择适当的开发工具和框

PHP入门教程:初学者必须掌握的基本语法PHP入门教程:初学者必须掌握的基本语法Jun 11, 2023 pm 09:45 PM

PHP被广泛应用于Web开发中,是一门服务器端脚本语言。在学习PHP之前,我们需要具备HTML和CSS的基础知识。下面将介绍初学者必须掌握的PHP基本语法。变量变量在PHP中使用$符号进行定义和赋值,如$variable=“helloworld”;。变量名必须以$符号开头,并且只能包含字母、数字和下划线。PHP中变量具有动态类型的特点,可以存储数字、字符串

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!