浅谈PHP语法(1)
作者:华红狼
正文:
《谈谈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所特有的,有时可使程序简单化。
--(待续)--

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

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.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
