Home php教程 php手册 浅谈PHP语法(1)

浅谈PHP语法(1)

Jun 13, 2016 pm 12:42 PM
html php author Basic I text of grammar

作者:华红狼
正文:
  《谈谈HTML语法》一文,我已经介绍了基本的HTML语法。可以编出一个静态的Web页,可动态交互信息是很重要的。如一些网站的会员制崐的会员注册、登录都需后端程序的运行。很多网站所用的CGI程序主要用Perl、ASP、Java、PHP编写,而我们所要用的就是PHP。它是完全免崐费的,这就要感谢那些默默无闻的编程人员了。PHP的结构类似于C语言,这可是应证了C语言里提的“一处学习,到处编程”。相信学过C崐语言的人可很容易上手PHP的。还是先介绍一些PHP语法吧。本文适合初学者学习。
  PHP与C语言也有一些差别,或者说在某种程度上可能比C语言更为灵活。在C语言中,变量要先定义,才能使用。而PHP中变量则不需崐事先定义,直接使用即可。对于变量的类型,在赋值时自动生成。PHP变量的类型分为:整数(int)、双精度型(double)、字符串(string)、崐数组(array)、对象(object)。
  整数大小超出其范围后,自动转化为双精度型,其值范围如下表:
┌─────┬─────┬──────┬────────────┐
│ 声明类型 │长度(位)│长度(字节)│    值的范围     │
├─────┼─────┼──────┼────────────┤
│  int  │  32  │  4   │-2147483647~2147483647 │
├─────┼─────┼──────┼────────────┤
│  double │  32  │  4   │ 1.7E-308~1.7E+308   │
└─────┴─────┴──────┴────────────┘
  字符串,通常用""(双引号)表示。也可用''(单引号)表示,如下:
  $a="abc";
  $b="abc$a";
  $c='abc$a';
  $d="\"cde\"";
  $e='"cde"';
  PHP中的各种变量均在变量名前加上“$”以示区别。
  注意,$b的内容为abcabc,$c的内容为abc$a,$d的内容为"cde",$e的内容也为"cde"。可以看出,双引号中的内容中的变量名会被替代崐,而单引号中的则不会。双引号中的内容需转义,如$应用\$表示,而单引号中的则不用。
  PHP中的数组语法为:
  数组名[索引]
  索引可为数字,也可为文字。但不建议使用文字,因为意义不大。对于数组也比其它语言灵活如下例:
$names[]=100;
$names[]=200;
$names[]="hi,how are you";
$names[]=98.5;
$names[]=1.7E+23;
$num=count($names);
for ($i=0;$i echo "$names[$i]
";
        }
?>
  可看出,一个数组中的元素不一定为同一类型,这就是PHP数组的“活”处。
  使用对象,可使编程者更易于维护,也使程序更为易读。较其它语言,PHP可简单多了,它只有类别(class)、方法(method)、属性(attr崐ibute)及扩展(extendsions)等。

  前文谈的只是PHP的数据类型,所谓“磨刀不误砍柴功”,打好PHP基础才能更好地学好PHP编程。
  PHP中的表达式与运算符与C语言的差别不大,现将其列表于下:
┌─────┬─────────┬──────────┐
│ 符 号 │   运算符   │  范 例     │
├─────┼─────────┼──────────┤
│  +   │   加法    │  $a+$b      │
├─────┼─────────┼──────────┤
│  -   │   减法    │  $a-$b      │
├─────┼─────────┼──────────┤
│  *   │   乘法    │  $a*$b      │
├─────┼─────────┼──────────┤
│  /   │   除法    │  $a/$b      │
├─────┼─────────┼──────────┤
│  %   │   取余数   │  $a%$b      │
├─────┼─────────┼──────────┤
│  ++   │   递增    │ $a++或++$a    │
├─────┼─────────┼──────────┤
│  --   │   递减    │ $a--或--$a    │
├─────┼─────────┼──────────┤
│  ==   │   等于    │ $a==10       │
├─────┼─────────┼──────────┤
│  ===  │   绝等于   │ $a===10      │
├─────┼─────────┼──────────┤
│  !=   │   不等于   │ $a!=10       │
├─────┼─────────┼──────────┤
│  ├─────┼─────────┼──────────┤
│  >   │   大于    │ $a>8        │
├─────┼─────────┼──────────┤
│  ├─────┼─────────┼──────────┤
│  >=   │  大于等于   │ $a>=1       │
├─────┼─────────┼──────────┤
│  =   │ 相等赋值运算符 │ $a=0        │
├─────┼─────────┼──────────┤
│  +=   │ 加法指定运算符 │ $a+=5       │
├─────┼─────────┼──────────┤
│  -=   │ 减法指定运算符 │ $a-=1       │
├─────┼─────────┼──────────┤
│  *=   │ 乘法指定运算符 │ $a*=2       │
├─────┼─────────┼──────────┤
│  /=   │ 除法指定运算符 │ $a/=5       │
├─────┼─────────┼──────────┤
│  %=   │ 余数指定运算符 │ $a%=7       │
├─────┼─────────┼──────────┤
│  .=   │ 字符串指定运算符│ $a.="hello"    │
├─────┼─────────┼──────────┤
│  &   │ 与       │ $a&$b       │
├─────┼─────────┼──────────┤
│  |   │ 或       │ $a|$b       │
├─────┼─────────┼──────────┤
│  ^   │ Xor       │ $a^$b       │
├─────┼─────────┼──────────┤
│  ~   │ 非        │~$a(取1的补码   )│
├─────┼─────────┼──────────┤
│  ├─────┼─────────┼──────────┤
│  >>   │ 向右移位     │ $a>>$b       │
├─────┼─────────┼──────────┤
│and或&&  │ 与       │$a and $b或$a&&$b  │
├─────┼─────────┼──────────┤
│or或||  │ 或       │$a or $b或$a||$b  │
├─────┼─────────┼──────────┤
│xor    │ Xor       │   $a xor $b   │
├─────┼─────────┼──────────┤
│  !   │ 非       │    !$a     │
└─────┴─────────┴──────────┘
┌───┬────────────┐
│符号 │ 意义说明       │
├───┼────────────┤
│ $  │变量          │
├───┼────────────┤
│ &  │变量的指针(加在变量前)│
├───┼────────────┤
│->  │对象的方法或属性    │
├───┼────────────┤
│=>  │数组的元素值      │
├───┼────────────┤
│? :  │三元运算符       │
└───┴────────────┘
  同C语言的比较一下吧。其中只是多了个“.”这一个运算符。它的作用是使两个字符串相连,如下例,显示结果为hello,my baby.
$a="hello,";
$b="my baby.";
echo $a.$b;
?>
  还有一个符号也使PHP的功能强大了。这就是“$”。它是用于变量之前的,表示这是个变量,如$A,$b等。那它的作用又强在哪呢?这崐就是变量的变量。
  如下例:
  $a="go";
$$a="here";
echo $a;
echo $$a;
echo $go;
?>
  显示结果为:
go
here
here
  其实,在一个变量前加一个“$”,就是把这个变量的内容作为了一个新的变量名。这是PHP所特有的,有时可使程序简单化。
--(待续)--

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

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,

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

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

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.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

See all articles