局部变量与全局变量
变量的存在有着它的生命周期,我们可以让它存在于一个小的函数内部,也可让它存在于整个程序当中。对于一般情况下声明的变量,我们称之为局部变量,只能在当前程序段中存在,而使用$globals声明出来的变量则是在当前页面整个程序当中都会有效。
例:
Copy to Clipboard引用的内容:[www.bkjia.com]
<?php<br /> $a=1;<br /> $b=2;<br /> function sum()<br /> {$a;<br /> $b;<br /> $b=$a+$b;<br /> }<br /> sum();<br /> echo$b;<br /> ?>
在这段程序当中,
第2至3行,我们建立了两个变量a和b并分别给其赋值为1和2。
第3行至第7行,我们定义了一个自加的函数sum(),其作用是让sum内部的变量a和b进行相加,并将加得的值赋给b。
第8行,调用sum函数。
第9行,用echo将b的值输出。
可能有人会认为此时在网页上输出的值一定是3,但是运行后你会发现,值还是为2,也就是b原先的值。这就是局部变量的原因造成的,在第2至3行当中声明的变量无法在sum()函数中使用,也就是说,sum函数中使用的a和b和第2至3行中a和b只是名字相同,但两者之间毫无关系。所以,在最终输出的b也就是第3行b的值了。
但如果,我们将程序修改下面的样式:
Copy to Clipboard引用的内容:[www.bkjia.com]
<?php<br /> $a=1;<br /> $b=2;<br /> function sum()<br /> {<br /> global $a,$b;<br /> $b=$a+$b;<br /> }<br /> sum();<br /> echo $b;<br /> ?><br /> <PRE><br /> <BR><BR>
我们发现,在sum函数中,我们给变量a和b加上了一个global修饰符,这个时候,a和b就和函数外的a和b建立上了关系,它们就是同一个变量了。因此,这个程序在运行的时候,其结果就为3了。因此,我们在声明全局变量时,只需在局部使用它们时(本例中是在函数sum中),给它们加上一个修饰符global,它们就可以继承外部的值,也就不再是局部变量了。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
