Home Backend Development PHP Tutorial PHP语法小结之基础和变量_PHP

PHP语法小结之基础和变量_PHP

May 29, 2016 am 11:47 AM
php syntax

最近有个H5项目的需求,需要服务端,考察过后决定用PHP实现一个HTTP服务端,于是开始重温PHP语法,顺便记录一下要点,以便随时重温。内容摘抄自w3school的PHP手册,并按照自己的理解重新进行了整理。

PHP是什么?

PHP 是 "PHP Hypertext Preprocessor" 的首字母缩略词
PHP 代码在服务器上执行,而结果以纯文本返回浏览器
PHP 文件能够包含文本、HTML、CSS 以及 PHP 代码
PHP 文件的后缀是 ".php"
PHP 脚本可放置于文档中的任何位置。
PHP 文件通常包含 HTML 标签以及一些 PHP 脚本代码。

PHP基础语法

脚本以 结尾
语句以分号结尾(;)
代码块的关闭标签也会自动表明分号(因此在 PHP 代码块的最后一行不必使用分号)。
// 或者 # 表示单行注释
/**/是多行注释
变量大小写敏感
用户定义的函数、类和关键词大小写不敏感(例如 if、else、echo 等等)都对

PHP常量

常量一旦被定义就无法更改或撤销定义
常量贯穿整个脚本是自动全局的
设置常量使用 define() 函数,它使用三个参数:

首个参数定义常量名称
第二个参数定义常量值
(可选)第三个参数规定常量名是否大小写敏感,默认是 false。

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

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

Copy after login

有效的常量名是以字符或是下划线开通

PHP变量

变量弱类型
变量以 $ 符号开头,其后是变量的名称,如 $x=5;
函数之外声明的变量拥有 Global 作用域,只能在函数以外进行访问。
函数内部声明的变量拥有 LOCAL 作用域,只能在函数内部进行访问。
global 关键词用于访问函数内的全局变量。要做到这一点,请在(函数内部)变量前面使用 global 关键词:
实例1:

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

 myTest();

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

Copy after login

实例2:

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

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

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

Copy after login

PHP Static关键词

通常,当函数完成/执行后,会删除所有变量。不过,有时我需要不删除某个局部变量。实现这一点需要更进一步的工作。
要完成这一点,请在您首次声明变量时使用 static 关键词:

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

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

Copy after login

然后,每当函数被调用时,这个变量所存储的信息都是函数最后一次被调用时所包含的信息。
注释:该变量仍然是函数的局部变量。

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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

5 basic concepts that PHP beginners must know 5 basic concepts that PHP beginners must know Jun 21, 2023 am 10:24 AM

With the continuous development of Internet technology, PHP, as a Web programming language, is becoming more and more popular. PHP is widely used and can be developed using PHP from simple static websites to large e-commerce websites. Whether you are a novice who is just starting to learn PHP or a developer who already has some experience, it is very necessary to master some basic concepts. In this article, I will introduce 5 basic concepts that PHP beginners must know. Variables in PHP are containers used to store data. In PHP,

Getting Started with PHP: Basic PHP Syntax Getting Started with PHP: Basic PHP Syntax May 20, 2023 am 08:39 AM

PHP is a server-side scripting language that is used to develop dynamic websites, web applications, and web programs. PHP has a wide range of applications, and both beginners and experienced developers can benefit from it. This article will provide you with an introductory guide to the basic syntax of PHP. If you want to learn PHP programming and build a solid foundation from scratch, you've come to the right place. The basic structure of PHP. A PHP program contains the following three parts: &lt;?php//PHP code?&gt;& on both sides of the code

How to solve common problems in PHP: syntax errors and warnings How to solve common problems in PHP: syntax errors and warnings Jun 11, 2023 pm 04:13 PM

PHP is a widely used server-side programming language often used to build dynamic web pages. However, when writing PHP scripts, you often encounter various syntax errors and warnings. These errors and warnings can cause your code to not run properly or cause problems, so it's important to resolve them. This article will introduce common problems in PHP: syntax errors and warnings, and provide effective methods on how to solve these problems. Syntax Errors Syntax errors are a very common problem when writing scripts in PHP. Syntax errors may be due to the following reasons

How to effectively improve the level of PHP programming? How to effectively improve the level of PHP programming? Jun 12, 2023 am 08:57 AM

PHP is a language widely used in web development, and its popularity and usage rate are quite high. Many beginners will encounter some difficulties when learning PHP programming, such as not knowing how to improve their programming level. Below we will introduce some methods to make it easier for you to improve your PHP programming level. Learn the latest technology Internet technology updates very quickly, and PHP is no exception. If you want to become an excellent PHP programmer, you must first learn the latest PHP technology and master the latest Web development technology, such as M

PHP from beginner to advanced -- learn basic syntax and concepts PHP from beginner to advanced -- learn basic syntax and concepts Sep 09, 2023 am 10:01 AM

PHP entry to advanced - learn basic syntax and concepts Introduction: PHP (Hypertext Preprocessor) is a popular server-side scripting language widely used in Web development. In this article, we will start from the entry level, gradually learn the basic syntax and concepts of PHP in depth, and provide code examples for your reference. 1. PHP installation and configuration Before starting to learn PHP, you first need to install PHP on your machine. You can download it from the official website (https://w

In-depth analysis of the functions and characteristics of PHP arrow symbols In-depth analysis of the functions and characteristics of PHP arrow symbols Mar 22, 2024 am 09:54 AM

The arrow symbol (-&gt;) in PHP is a very important operator used to access the properties and methods of objects. This article will provide an in-depth analysis of the functions and characteristics of PHP arrow symbols, and provide specific code examples to help readers better understand their usage. In PHP, the arrow symbol (-&gt;) is mainly used to access the properties and methods of objects. When we create an object, we can access the object's properties or call the object's methods through arrow symbols. First, let's look at a simple example, create a class and instantiate

PHP development experience sharing: tips and suggestions for building efficient functions PHP development experience sharing: tips and suggestions for building efficient functions Nov 22, 2023 am 10:45 AM

PHP is a widely used server-side scripting language for developing dynamic web pages and web applications. Many websites and applications use PHP as their back-end development language, so having PHP development skills and experience is crucial to building efficient functionality. In this article, I will share some experiences and tips I have learned in PHP development, aiming to help developers improve their coding skills and ability to build efficient functions. Pick the right development tools and frameworks Before starting PHP development, pick the right development tools and frameworks

PHP introductory tutorial: basic syntax that beginners must master PHP introductory tutorial: basic syntax that beginners must master Jun 11, 2023 pm 09:45 PM

PHP is widely used in web development and is a server-side scripting language. Before learning PHP, we need to have basic knowledge of HTML and CSS. The following will introduce the basic syntax of PHP that beginners must master. Variables Variables are defined and assigned using the $ symbol in PHP, such as $variable="helloworld";. Variable names must start with the $ symbol and can only contain letters, numbers, and underscores. Variables in PHP have the characteristics of dynamic type and can store numbers and strings.

See all articles