Home Backend Development PHP Tutorial phalcon?? PHP基础知识(一)

phalcon?? PHP基础知识(一)

Jun 23, 2016 pm 01:58 PM

一、变量和常量

1.1、变量名(标示符)

1)变量:$开头标志
2)变量名:可以由字母,数字,_ 3者组成,不能用数字开头

3)标识符是区分大小写的,但函数名不区分大小写。

4)变量名称可以与函数名称相同,虽然是允许的,但应尽量避免混淆。

PHP不要求在使用变量之前声明变量,当第一次给一个变量赋值时,才创建了这个变量。

例如:

$3age;//错$_ = 6;//可以$*p = 30;//错
Copy after login


1.2、数据类型

1) Integer: 用来表示整数。

2) Float: 用来表示所有实数。

3) String: 用来表示字符串。

4) Boolean: 用来表示true或false。

5) Array: 用来保存具有相同类型的多个数据项。

6) Object: 用来保存类的实例。

PHP可以在任何时间根据保存在变量中的值来确定变量的类型,当需要强制类型转换时可以按照java的语法来转换

例如:

$icount = 0;$fcount = (float)$icount;
Copy after login

1.3、常量的定义

常量可以保存一个值,但是程序一旦初始化之后,常量的值就不能改变。

define('PI', 3.14159);echo PI;
Copy after login

常量一般用大写表示,用define函数定义,并且不用$符号标识。


1.4、变量作用域

作用域指在一个脚本中某个变量可以使用或可见的范围,PHP具有6项基本的作用域规则。

1) 内置超级全局变量可以在脚本中的任何地方使用。

例如:

$GLOBALS: 所有全局变量数组。$_SERVER: 服务器环境变量数组$_GET: GET方法传递给该脚本的变量数组$_POST: POST方法传递给该脚本的变量数组$_COOKIE: cookie变量数组$_FILES: 与文件上传相关的变量数组$_ENV: 环境变量数组$_REQUEST: 所有用户输入的变量数据,包括$_GET、$_POST和$_COOKIE$_SESSION: 会话变量数组
Copy after login

2) 常量一旦被声明,可以在全局可见。

3) 在脚本中声明的全局变量在全脚本中是可见的。

4) 函数内部使用的变量声明为全局变量时,名称要与全局变量名称一致。

5) 函数内部创建并声明为静态的变量在函数外不可见,但在函数的多次执行过程中保持该值。

6) 函数内部创建的变量是本地的,当函数终止时,该变量也就不存在。


二、运算符或操作符

2.1、算数运算符

算术操作符也就是数字操作符,通常用于整型或双精度类型的数据。如果应用在字符串中,PHP会试图将这些字符转换成一个数字,如果其中包含"e"或"E",它会被当作是科学表示法并被转换成浮点数,否则将会被转换成整数。PHP会在字符串开始处寻找数字,并且使用这些数字作为该字符串的值,如果没有找到数字,则该字符串的值为0。

操作符 名称 示例
+ $a + $b
- $a - $b
* $a * $b
/ $a / $b
% $a % $b






2.2、字符串连接

利用“.”

$string = "hello"." world";
Copy after login

2.3、赋值运算

复合赋值操作符
操作符 使用方法 等价于
+= $a += $b $a = $a + $b
-= $a -= $b $a = $a - $b
*= $a *= $b $a = $a * $b
/= $a /= $b $a = $a / $b
%= $a %= $b $a = $a % $b
.= $a .= $b $a = $a . $b








2.4、引用

引用操作符"&"可以在关联赋值中使用。引用相当于一个别名,而不是一个指针,它使两个变量指向相同的内存地址,可以使用unset来重置

例如:

$a = 1;$b = &$a;$b = 2;unset($a);
Copy after login

2.5、比较运算

比较操作符
操作符 名称 使用方法
== 等于 $a == $b
=== 恒等 $a === $b
!= 不等 $a != $b
!== 不恒等 $a !== $b
不等 $a $b
小于 $a
> 大于 $a > $b
小于等于 $a
>= 大于等于 $a >= $b










2.6、逻辑运算

逻辑操作符
操作符 名称 使用方法
! !$b
&& $a && $b
|| $a || $b
and $a and $b
or $a or $b
xor 异或 $a xor $b








2.7、位运算

位操作符
操作符 名称 使用方法
& 按位与 $a & $b
| 按位或 $a | $b
~ 按位非 ~$a
^ 按位异或 $a ^ $b
左位移 $a
>> 右位移 $a >> $b








2.8、其他

1) 逗号操作符

逗号操作符","用来分隔函数参数和其他列表项,这个操作符经常被附带使用。

2) 类操作符

"new"和"->"分别用来初始化类的实例和访问类的成员。

3) 三元操作符

三元操作符"? :"类似于条件语句if-else的表达式版本,语法格式如下:

condition ? value if true : value if false
Copy after login

4) 错误抑制符

错误抑制符"@"可以在任何表达式前使用,使用这个操作符,可以抑制代码产生的警告。

5) 执行操作符

执行操作符是一对操作符,它是一对反向单引号"` `",PHP将试着将反向单引号之间的命令当作服务器端的命令行来执行,表达式的值就是命令的执行结果,例如:

echo `ls -l`;
Copy after login


6) 类型操作符

"instanceof"操作符允许检查一个对象是否是特定类的实例,例如:

if ($object instanceof sampleClass)  echo "Object is an instance of sampleClass";
Copy after login

2.9、操作符优先级以及结合性

由低到高如下:

操作符优先级
结合性 操作符
,
or
xor
and
print
= += -= *= /= .= %= &= != ^= ~= >=
? :
||
&&
!
^
&
不相关 == != === !==
不相关 >=
>
+ - .
* / %
! ~ ++ -- (type) @
[ ]
不相关 new
不相关 ()




















三、测试函数与测试变量

3.1、PHP提供了特定类型的测试函数。

例如:

1) is_array(): 检查变量是否是数组。

2) is_double()、is_float()、is_real(): 检查变量是否是浮点数,所有函数相同。

3) is_long()、is_int()、is_integer(): 检查变量是否是整数,所有函数相同。

4) is_string(): 检查变量是否是字符串。

5) is_bool(): 检查变量是否是布尔值。

6) is_object(): 检查变量是否是一个对象。

7) is_resource(): 检查变量是否是一个资源。

8) is_null(): 检查变量是否为null。

9) is_scalar(): 检查变量是否是标量,即整数、布尔值、字符串或浮点数。

10) is_numeric(): 检查变量是否是数字或数字字符串。

11) is_callable(): 检查变量是否是有效的函数名称。


3.2、测试变量状态

PHP有几个函数用来测试变量状态。

例如:

1) isset()

bool isset(mixed var[, mixed var[, ...]])
Copy after login

issset()需要一个变量作为参数,如果这个变量存在,则返回true,否则返回false。也可以传递一个由逗号间隔的变量列表,如果所有变量都被设置了,即返回true。

还可以使用与isset()相对应的unset()来销毁一个变量:

void unset(mixed var[, mixed var[, ...]])
Copy after login

2) empty()

bool empty(mixed var)
Copy after login

empty()可以用来检查一个变量是否存在,以及它的值是否为非空和非0,相应的返回值为true或false。

四、控制语句

4.1、if else

if (condition) {  expression;}elseif (condition) {  expression;}else {  expression;}
Copy after login

4.2、switch

switch (expression) {  case value1:    expression;    break;  case value2:    expression;    break;  defalut:    expression;    break;}
Copy after login

switch语句工作方式类似于if语句,但是它允许条件可以有多于两个的可能值。在switch语句中,只要条件是一个简单的数据类型,可以提供一个case语句来处理每一个条件值,并且提供相应的动作代码,此外还有一个默认的case条件来处理没有提供特定值的情况。

当switch语句中的特定case被匹配时,PHP将执行该case下的代码,直至遇到break语句,如果没有break语句,switch将执行这个case以下所有值为true的case中的代码。

4.3、while

while (condition) {  expression;}
Copy after login

4.4、for

for (expression1; condition; expression2) {  expression3;}
Copy after login

expression1在开始时只执行一次,通常在这里设置计数器的初始值。在每一次循环开始之前,condtion表达式将被测试,如果表达式的值为false,循环将结束。expression2在每一次循环结束时执行,通常在这里调整计数器的值。expression3在每一次循环中执行一次。

4.5、do while

do {  expression;}while (condition);
Copy after login

4.6、跳出控制结构

如果希望停止一段代码的执行,可以有3种方法。

1) 如果希望终止一个循环,可以使用break语句,脚本会从循环体后面的第一条语句开始执行。

2) 如果希望跳到下一次循环,可以使用continue语句。

3) 如果希望结束整个PHP脚本的执行,可以使用exit语句。



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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles