PHP基本语法第二章
一,如何定义一个常量
关键字:define 语法define('常量名','常量的值')
$a=123; define('I',$a); echo I; ?> 二,数组
1定义一个数组
关键字array 语法array(key=>value,key2=>value2,key3=>value3) key可以是整形或字符串,value可以是任意值
$arr=array('too'=>'bar',123=>true); echo $arr['too'].'
'; echo $arr[123]; ?>
打印数组的方法print_r 关键字:print_r 语法print_r(要打印数组名),主要用于调试
header("Content-Type:text/html; charset=utf-8"); $stu=array('stu_no'=>'10010','stu_name'=>'老赵'); print_r($stu); ?> 输出结果Array ( [stu_no] => 10010 [stu_name] => 老赵)
输出方法echo $数组名[key]
header("Content-Type:text/html; charset=utf-8"); $stu=array('stu_no'=>'10010','stu_name'=>'老赵'); echo $stu['stu_no'].'
'; echo $stu['stu_name']; ?> 输出结果
10010
老赵
另外一种定义数组的方法 直接输入value值key为从0起排列的整形
array('value','value2','value3','value4')
header("Content-Type:text/html; charset=utf-8"); $stu=array('皮皮','乐乐','教皇','老赵'); print_r($stu); ?> 输出结果为Array ( [0] => 皮皮[1] => 乐乐[2] => 教皇[3] => 老赵)
分别输出:
header("Content-Type:text/html; charset=utf-8"); $stu=array('皮皮','乐乐','教皇','老赵'); echo $stu[0].'
'; echo $stu[1].'
'; echo $stu[2].'
'; echo $stu[3].'
'; ?> 输出结果
皮皮
乐乐
教皇
老赵
循环输出方法for语句 语法
for(循环条件) 例:$i=0;$i
{
echo $数组名[循环变量名]
}
header("Content-Type:text/html; charset=utf-8"); $stu=array('皮皮','乐乐','教皇','老赵'); for ($i=0;$i'; } ?> 输出结果
皮皮
乐乐
教皇
老赵
while循环 语法
$条件变量名=条件变量值
while(条件语句) 例子$i
{
$数组名[$条件变量名];
$条件变量++
}
header("Content-Type:text/html; charset=utf-8"); $stu=array('皮皮','乐乐','教皇','老赵'); $i=0; while ($i 输出结果 皮皮 乐乐 教皇 老赵
在数组末尾添加元素
语法$数组名=array[value,value1,value2,value3];
$数组名[]=要添加的value;
$数组名[]=要添加的value;
header("Content-Type:text/html; charset=utf-8"); $stu=array('皮皮','乐乐','教皇','老赵'); $stu[]='浩民'; $stu[]='苏超'; $stu[]='吕腾'; for ($i=0;$i 输出结果 皮皮 乐乐 教皇 老赵 浩民 苏超 吕腾
创建一个范围的数组range和count取得数组里有多少元素的方法
语法$数组名=range(范围开始,范围结束)
count($数组名)
header("Content-Type:text/html; charset=utf-8"); $stu=range(1,12); for ($i=0;$i
header("Content-Type:text/html; charset=utf-8"); $stu=range('a','z'); for ($i=0;$i
三 填充数组
array_pad
语法
array_pad($数组名,数组长度,填充默认值)
header("Content-Type:text/html; charset=utf-8"); $stu=range('0','3'); $stu2=array_pad($stu,7,0); for ($i=0;$i
五,在数组中删除和插入替换元素array_splice
array_splice接两个参数代表删除 接四个参数代表插入或替换(第三个参数为0的时候为插入,不为0时为替换)
删除语法array_splice($数组名,删除结束下标) 下标等于在这个Key之前的删除
header("Content-Type:text/html; charset=utf-8"); $stu=range('0','12'); $stu2=array_splice($stu,5); for ($i=0;$i
插入语法array_splice($被插入数组名,下标,0,$插入的新数组名) 下标为在这个下标到上一个之前插入新数组
header("Content-Type:text/html; charset=utf-8"); $stu=range('0','12'); $stu2=array('a','b','c'); array_splice($stu,5,0,$stu2); for ($i=0;$i
替换语法array_splice($被替换数组名,key,key2,$替换的新数组名) 下标1下标2结合在一起表示从下标key开始数下标key2个元素被新的数组替换
header("Content-Type:text/html; charset=utf-8"); $stu=range('0','12'); $stu2=array('a','b','c'); array_splice($stu,3,6,$stu2); for ($i=0;$i
从3开始数6个元素被替换成a,b,c
本文出自 “PHP学习笔记” 博客

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

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

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

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,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.
