search
Homephp教程php手册浅谈PHP语法1

浅谈PHP语法1

Jun 13, 2016 am 10:27 AM
htmlphpCanBasicIofgrammar

《谈谈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)等。
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

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

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.