PHP移动互联网开发笔记(2)——变量及常量
一、PHP5.4的基本语法格式
view source print?1、PHP的分割符
1.
$php
=true;
//分号结束语句
2.
if
(
$php
){
3.
echo
真
;
//分号结束语句
4.
}
//大括号结束语句
5.
?>
二、PHP5.4的变量与变量的数据类型
2、PHP注释与语法标识
(1)、单行注释 // 来源于C++的注释 # 来源于C语言的注释
(2)、多行注释 /* */ 来源于C语言的注释
3、函数的使用格式
(1) 返回值 函数名()
(2) 返回值 函数名(参数, 参数)
(3) 函数名(参数, 参数, 返回变量)
(4) 返回值 函数名(.. ..)通用符 // PHP5.4的用法
变量是由一个美元符“$开头,”$后是一个标识符。标识字符串只字母、数字、下划线组成且不可以数字开头。
view source print?01.
$php
=true;
//分号结束语句
02.
if
(
$php
){
03.
echo
真
;
//分号结束语句
04.
}
//大括号结束语句
05.
06.
$url
=
blog.csdn.net/dawanganban
;
//定义变量
07.
echo
$url
;
08.
unset(
$url
);
//删除一个变量url
09.
echo
$url
;
10.
?>
变量命名的方式
(1)单词之间直接连接
$titlekeyword
(2)单词之间用下划线连接
$title_keyword
(3)单词之间首字母大写(驼峰)
$titleKeyword
PHP的数据类型如下:
(1)字符串(String):单引号(简单引号)或双引号(功能引号)内的内容
(2)整型(integer): -2^32
(3)浮点符(float或double) 1.8E+308 (1.8 x 10^308)
(4)布尔型(boolean) true或false
(5)数组(Array)
(6)对象(Object)
view source print?01.
class
Person{
02.
public
$userName
=
阳光小强
;
03.
public
function
getMsg(){
04.
echo
姓名为:
.
$this
->userName;
05.
}
06.
}
07.
$p
=
new
Person();
08.
$p
->getMsg();
09.
10.
?>
(7)资源类型(Resouce) 系统数据资源
资源是一个特殊的数据类型,无法直接获得变量,需要通过专门的函数来访问:
数据库访问必须通过Mysql函数库、Mysqli函数库或PDO函数库实现。
文件访问必须通过FileSystem函数库实现。
目录操作必须通过Directory函数库实现。
图像操作必须通过GD函数库实现。
(8)空值(NULL)
三、PHP5.4的系统常量与自定义常量
常量在程序执行期间无法改变数据,常量的作用域是全局的。常量的命名与变量相似,只是不带“$符号。一个有效的常量由字母或者下划线开头,一般在PHP中常量都为大写字母而且又分为系统常量和自定义常量。
系统常量范例:
__FILE__ 默认常量,是指PHP程序文件名及路径
__LINE__ 默认常量,是指PHP程序的行数
__CLASS__ 类的名称
在PHP中通过define()函数来定义一个常量,其语法格式为:
bool define(string $name, mixed $value [, bool case_$insensitive])
name:常量的名称
value:常量的值
insensitive:指定常量名称是否区分大小写。如果设置为true则不区分大小写;如果设置为false则区分大小写,默认值为false。
view source print?
1.
define(
COLOR
,
red
);
//定义一个常量COLOR,值为red
2.
echo
COLOR.
3.
;
//输出常量COLOR的值
可变变量
view source print?
1.
$a
=
b
2.
$
$a
=
123
//可变变量
3.
echo
$b
;
输出结果为:123
在字符串中输出变量要使用双引号
view source print?
1.
$a
=50;
2.
//echo '我有$a元人民币; 单引号
3.
echo
我有$a元人民币
;
在双引号中可以执行的转意字符更多,比如
判断数据类型
view source print?
1.
$a
=
-5
;
2.
//$a=-5;
3.
var_dump(
$a
);

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use