search
HomeBackend DevelopmentPHP Tutorial《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组_PHP

数字、浮点、布尔型是值类型,英文:int、float、bool,这样你知道他们怎么用了。

比如语句:$fa=3.14;

字符串和数组是引用类型,也就是说他们作为地址放在栈里,当重新赋值时,栈里的地址改变方向,原来的指向变没或给回收了,英文:string、array。

比如:$str=”字符串”;$arr=array(“a”=>”数”,”b”=>”组”);// array()是一个数组赋值函数,这样的函数PHP有一千多个,一般常用的不到200个,我觉得。

字符串操作:

实例2:字符串合并、相加
复制代码 代码如下:
$str = 1;
echo $str .= ""; //数字转化为字符串再合并,结果:"1"。
echo "
";
echo $str += "1元"; //字符串转化为数字再相加,如"1XXX"转化为数字1,结果: 2。
echo "
";
?>

实例3:字符串改变大小写
复制代码 代码如下:
$str="12345ABc";
echo strtolower($str);//变小写,结果:"12345abc"。
echo "
";
echo strtoupper($str);//变小写,结果:"12345ABC"。
echo "
";
?>

实例4:字符串长度、截取子字符串(中英文)
复制代码 代码如下:
$str = "字符串2";
echo mb_strlen($str, "UTF-8"); //返回字符串长度的函数,第二个参数是编码,由于页面用UTF-8编码,所以为这样。如果省去,返回内存占用的字节数(ASCII),即10。结果4
echo "
";
echo mb_substr($str, 1, 2, "UTF-8"); //返回字符截取,1为从”符”地址开始截取,2为截取2个"UTF-8"编码的字符,结果:“符串”。
echo "
";
/**
* 知识点:现在开始接触函数了,每个函数都有()作为堆栈调用,()里面放0个或多个参数,可以自定义可以有默认值。而关键字比如echo是没有()的。
* 很多书用GB2312编码,取长度和子串时很麻烦。下面给大家参考一下不用上面的mb中文字符串扩展库实现原理:
*/
function my_mb_strlen($str, $code = "UTF-8") // 定义一个新函数,$str是必须传入的参数。
{$num= 0;
if ($code == "UTF-8")
{
$str = iconv("UTF-8", "GB2312", $str); //转化为GB2312编码,ord函数返回对应的ASCII值判断每个字节该中文字符是否结束。
for($i = 0;$i {
if (ord($str[$i]) > 0xa0)$i++; //$str[$i]对应内存的i字节。如果直接用UTF-8判断会复杂些,因为编码的多样性UTF-8是网页常用编码,UTF-16(Unicode)是windows编码。
$num++;
}
}
else
{
$num = "编码未实现";
} //有兴趣的自己查资料吧
return $num;
}
echo my_mb_strlen($str) . ";" . my_mb_strlen($str, "GB2312") . "
"; //该页编码用UTF-8,你却说传入的字符串3是GB2312,就算函数实现了也无法正确的。
?>

实例5:子字符串查找、替换

复制代码 代码如下:
$str = "字符串4";
echo mb_strpos($str, '串4', 0, "UTF-8"); //查找从0开始找到的第一个子字符串位置,结果:2。如果查找不到,返回空(="");如果最后两个参数不要,返回6。
echo "
";
echo mb_strstr($str, '串', 0, "UTF-8"); //截取从0开始找到的第一个子字符串至结尾,结果:"串4"。如果查找不到,返回空(="");如果最后两个参数不要,返回相同=strstr($str,'串')。
echo "
";
echo str_replace("4", "不是4", $str) ; //字符串替换,结果: "字符串不是4"。
echo "
";
?>

实例6:子字符串去空、html转义
复制代码 代码如下:
$str=" 字符串5 ";
echo $str=trim($str);//去除两边空格,结果:"字符串5"。
echo "
";
echo "color=\"red\"";//\手工转义里面的'、"、\,使之存储到内存,结果"color="red""
echo "
";
$str="
123";
echo htmlentities($str) ; //字符串转义&'"避免和html标识冲突,使之能在html浏览器端显示出来,结果:"<br>123"。
echo "
";
?>

Statement
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
PHP vs. Python: Understanding the DifferencesPHP vs. Python: Understanding the DifferencesApr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: Is It Dying or Simply Adapting?PHP: Is It Dying or Simply Adapting?Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The Future of PHP: Adaptations and InnovationsThe Future of PHP: Adaptations and InnovationsApr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

When would you use a trait versus an abstract class or interface in PHP?When would you use a trait versus an abstract class or interface in PHP?Apr 10, 2025 am 09:39 AM

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

What is a Dependency Injection Container (DIC) and why use one in PHP?What is a Dependency Injection Container (DIC) and why use one in PHP?Apr 10, 2025 am 09:38 AM

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Apr 10, 2025 am 09:37 AM

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

How does PHP handle file uploads securely?How does PHP handle file uploads securely?Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?Apr 10, 2025 am 09:33 AM

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

See all articles

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

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools