Home Backend Development PHP Tutorial 笨鸟学php(二) 怎么使用变量

笨鸟学php(二) 怎么使用变量

Jun 13, 2016 pm 12:02 PM
nbsp php quot var

笨鸟学php(二) 如何使用变量

一. 变量的声明

1. php中声明变量的方法:  $var = 123;

2. php是弱类型的语言, 变量的类型由存储的值决定

    注: Java是强类型语言, 声明变量一定要先指定类型

3. isset() 和 unset()

    isset() : 判断值是否存在;    unset() : 去掉变量的值

<?php $a = $b = $c = $d = "hello, world"; 	unset($a); 	if(isset($a)){ 		echo $a; 	}else{ 		echo "not exists"; 	}?>
Copy after login

二. 变量的命名

1.  变量前一定要使用"$", 声明和使用都要有这个符号.

2. 不能以数字开头.

3. 不能使用php中的运算符号+ - * / % &

4. 可以使用系统关键字作为变量名

5. php中只有变量和常量区分大小写, 其他不区分


二. 可变变量

1.  一个变量的变量名可以动态设置和使用

<?php $one = "aaa";	$two = "one";	$three = "two";	echo $$$three;    // 结果为aaa?>
Copy after login
2. 变量可以进行引用赋值(和Java中的引用一个)

<?php $one = "aaa";	$two = &$one;   // 使用"&"符号加到将要赋值的变量前, $one和$two指向同一块地址	$two = "bbb";	echo $one;?>
Copy after login

三. 变量的类型

1. 前面已经说过php是弱类型语言, 它的类型由存储的值决定

2. php有8中类型:

2.1) 四种标量:

       整形: int integer    布尔型: bool  boolean    浮点型: float  double  real    字符串: string

2.2) 两种复合类型

       数组: array    对象: object

2.3) 两中特殊类型

       资源类型: resource    空类型: null

<?php $var=10;	var_dump($var); // int(10)		$var=34.5;	var_dump($var); // float(34.5)		$var="abc";	var_dump($var); // string(3) "abc"		$var=true;	var_dump($var); // bool(true)	$var=array(1,2,3);	var_dump($var); // array(3){[0]=>int(1) [1]=>int(2) [2]=>int(3)}	$var=new mysqli("localhost", "root", "root", "newcms");	var_dump($var); // object(mysqli)#1(0){}	$var=fopen("1.php", "r");	var_dump($var); // resource(3) of type (stream)	$var=null;	var_dump($var); // NULL?>
Copy after login

四. 各种类型变量声明

<?php // 整数的最大值 4字节, 2的32次方, 214483647	$int=10;   //十进制声明	$int=045;  //以0开头的表示以8进制声明一个变量	$int=0xff; //以0x或0X开头的表示以16进制声明一个变量 0-9 a-f 0X A-F	$int=-5;   	$float=10;	$float=-10;	$float=3.14E5;	$float=3.15E+5;	$float=5.14E-2;  //E可以大写也可以小写	// 以下都是false的情况	$bool=false;   // true	$bool=0;       // 非0的数	$bool=0.000;   // 有非0的数出现	$bool="";      	$bool=" ";     	$bool=null;    // 非空代表	$bool="0";     // 非空非0的字符串	$bool=array(); // 有成员的数组	// 表示true的情况: true, 非0的数, 有非0的数出现, 非空代表, 非空非0的字符串, 有成员的数组	// 字符串的声明有多种方法	// 1.单引号和双引号都可以声明字符串, 声明的字符串没有长度限制	// 2.在双引号的字符串中, 即可以直接解析变量, 又可以直接使用转义字符	// 3.在单引号的字符串中, 不可以解析变量, 也不可以使用转义字符(可以以转义单引号本身, 也可以转义转义字符"\")	// 4.在双引号中不能再使用双引号, 在单引号中不能再使用单引号	$str=&#39;aaaaa\&#39;aaa&#39;;	$str="aaaa\"aaaa";	$str=<<<one	 one;	$str=`dir`;?>
Copy after login






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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles