PHP移动互联网开发笔记(2)变量及常量
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

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



Having no internet connection on your Windows 11 computer in Safe Mode with Networking can be frustrating, especially when diagnosing and troubleshooting system issues. In this guide, we will discuss the potential causes of the problem and list effective solutions to ensure you can access the internet in Safe Mode. Why is there no internet in safe mode with networking? The network adapter is incompatible or not loading correctly. Third-party firewalls, security software, or antivirus software may interfere with network connections in safe mode. Network service is not running. Malware Infection What should I do if the Internet cannot be used in Safe Mode on Windows 11? Before performing advanced troubleshooting steps, you should consider performing the following checks: Make sure to use

A constant is also called a variable and once defined, its value does not change during the execution of the program. Therefore, we can declare a variable as a constant referencing a fixed value. It is also called text. Constants must be defined using the Const keyword. Syntax The syntax of constants used in C programming language is as follows - consttypeVariableName; (or) consttype*VariableName; Different types of constants The different types of constants used in C programming language are as follows: Integer constants - For example: 1,0,34, 4567 Floating point constants - Example: 0.0, 156.89, 23.456 Octal and Hexadecimal constants - Example: Hex: 0x2a, 0xaa.. Octal

Constants and variables are used to store data values in programming. A variable usually refers to a value that can change over time. A constant is a type of variable whose value cannot be changed during program execution. There are only six built-in constants available in Python, they are False, True, None, NotImplemented, Ellipsis(...) and __debug__. Apart from these constants, Python does not have any built-in data types to store constant values. Example An example of a constant is demonstrated below - False=100 outputs SyntaxError:cannotassigntoFalseFalse is a built-in constant in Python that is used to store boolean values

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

A constant variable is a variable whose value is fixed and only one copy exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program. Unlike other languages, Java does not directly support constants. However, you can still create a constant by declaring a variable static and final. Static - Once you declare a static variable, they will be loaded into memory at compile time, i.e. only one copy will be available. Final - Once you declare a final variable, its value cannot be modified. Therefore, you can create a constant in Java by declaring the instance variable as static and final. Example Demonstration classData{&am

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:
