Home Backend Development PHP Tutorial PHP入门学习笔记之一_PHP

PHP入门学习笔记之一_PHP

Jun 01, 2016 pm 12:17 PM
study notes

1. 基本语法
要在HTML代码中嵌入PHP脚本的方法是在中书写代码。向浏览器输出数据采用echo或者print函数。echo可以接受多个参数,print 只能接受一个。echo的形式是

void echo(string arg1,[,…string argn]);

PHP语法允许省略括号。 例如,
复制代码 代码如下:
$my =' my ';
echo 'Hello',$my,'world'
?>

将会在浏览器上输出 Hello my world

PHP 还支持一个和C语言很像的printf函数,例如 printf(‘ %d apples',100),将会输出 100 apples。sprintf的用法和printf一致,不过它不是输出到浏览器,而是返回一个字符串。

2.数据类型和变量

PHP是弱类型的,一个变量不需要预先声明,也不需要指定类型。PHP中变量是$加上变量名,PHP的变量是区分大小写的。 例如上例中的 $my='my'。

PHP支持的变量类型包括:布尔型,整型,浮点型,字符串,数组和对象。前四种很常用,也和其他语言类似,不多做介绍。数组和对象后文具体介绍。

PHP中有函数来检测 对象的类型,它们是 getttype。gettype返回一个string,它的值可以是array,boolean,double,integer,object,resource,string 和 unknow type. PHP也支持显式的类型转换,语法和C类似。

转换操作符 转换为
(array) 数组
(bool) (boolean) 布尔型
(int) (integer) 整数
(object) 对象
(float),(double),(real) 浮点数
(string) 字符串

例如:
复制代码 代码如下:
$str ='a string';
$num=15;
$numstr='123.3';
echo gettype($str),'
';
echo gettype($num),'
';
echo gettype($numstr),'
';
$numstr=(float)$numstr;
echo gettype($numstr);
?>

输出结果为:

string
integer
string
double

还有函数可以用来判断一个变量是不是某种类型,例如 is_array(),is_bool()等等,用法都类似。

3. 函数和变量作用域
PHP声明函数的方法很简单,形式如下:
复制代码 代码如下:
function functionName(parameters){

function body

}

不需要指定返回类型,括号中也不需要指定变量类型,只要有变量名就可以。例如:
复制代码 代码如下:
function taxedPrice($price,$taxrate){
return $price*(1+$taxrate);
}
echo taxedPrice(100, 0.03);
?>

默认情况下,PHP是按值传递参数的,在函数内改变参数的值并不会改变函数外变量的值,但是PHP也支持按引用传递,语法和C一致,&$paramName,例如,下面一个经典的例子:
复制代码 代码如下:
function swap1($x,$y){
$t=$x;$x=$y;$y=$t;
}
function swap2(&$x,&$y){
$t=$x;$x=$y;$y=$t;
}
$a=3;$b=5;
swap1($a,$b);
printf("a is %d, b is %d
",$a,$b);
swap2($a,$b);
printf("a is %d, b is %d
",$a,$b);
?>

输出结果:

a is 3, b is 5
a is 5, b is 3

PHP的函数还支持参数的默认值,语法和C也是一样的。例如:
复制代码 代码如下:
function taxedPrice($price,$taxrate=0.03){
return $price*(1+$taxrate);
}
echo taxedPrice(100);
?>

下面介绍变量的作用域。PHP的变量作用域和C很类似,有局部变量,函数参数,全局变量,静态变量4种。局部变量就是在函数内声明的变量,函数参数是在函数首部声明的变量;不在函数中声明的变量是全局变量,全局变量可以在任何地方访问到,但是和C不同的是,如果在函数中要修改全局变量的值需要用GLOBAL关键字显式指定它是全局变量,否则PHP就会声明一个同名的局部变量并且覆盖它。例如:
复制代码 代码如下:
$taxrate=0.03; //global
function change1() {
$taxrate+=1;
}
function change2() {
GLOBAL $taxrate;
$taxrate+=1;
}
change1();
echo $taxrate,'
';
change2();
echo $taxrate,'
';
?>

输出的结果是:

0.03

1.03

PHP还有一种超级全局变量。超级全局变量是由PHP系统预定义的,主要用来访问与环境有关的信息,例如当前用户会话,用户操作环境和本地环境等。超级全局变量是一个数组,例如$_SERVER中存储了服务器相关的信息。$_GET,$_POST,$_FILES,$_COOKIE中分别存储了客户端采用get提交,和post的提交的信息,上传的文件,cookie信息等。这些变量的使用都很简单,需要什么信息只需要查找

4 变量的变量
和C一类的静态语言不同,PHP的变量名本身可以是一个变量,这对于需要动态生成很多变量的时候是很便利的。例如:
复制代码 代码如下:
$r="hello";
$$r="I am hello";
echo $hello;
?>

输出结果为: I am hello


5.流程控制语句
主要包括,if else, while,for,do while,switch. 这些和C语言都很像,基本是一致的。不多做介绍。有些不同,PHP的 elseif是一个关键字,是连在一起的,而C语言是else if。

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

Video Face Swap

Video Face Swap

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

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles