PHP和js数组、字符串、函数不同点基础总结
来源:互联网 发布:中式台球杨帆 知乎 编辑:程序博客网 时间:2024/06/06 13:13
一、在js中,函数内部可以访问到函数外部定义的变量,而在php中不同
$test = 123; abc(); //这里什么都不输出,因为访问不到$test变量function abc(){ echo($test); }
可以通过下面的方法在函数内部访问函数外部的变量
//1、传参进去$test = 123; abc($test); //123function abc($test){ echo($test); }//2、定义全局变量$test = 123; abc(); //123function abc(){global $test; echo($test); }//3、同上global $test; $test = 123; abc(); //123function abc(){global $test; echo($test); }
二、字符串操作
1、指定字符在字符串中首次出现的位置,如果找不到返回false
echo strpos("Hello world!","world"); //6
//在js中,找不到返回-1
var str = "Hello world!";
console.log(str.indexOf("world"); //6
2、截取指定长度的字符串 --> substr(原字符串,起始位置,截取的长度)
echo substr("Hello world",6); //world
//js中 --> 原字符串.substr(起始位置,截取长度)
var str = "Hello world!";
console.log(str.substr(3)) //lo world!
//在js中有substring方法截取两个下标之间的字符串,在php中没有这个方法,可以用substr的代替实现
3、字符串长度
echo strlen("Hello world!"); //12
//js中
console.log(str.length); //12
4、切割字符串为数组
$str = "Hello world";
echo explode(" ",$str); //-->Array ( [0] => Hello [1] => world)
//js中
console.log(str.split(' ')); //["Hello", "world"]
三、数组操作
1、创建索引数组
$cars = array("Volvo","BMW","Toyota"); //等于 array("0"=>"Volvo","1"=>"BMW","2"=>"Toyota");
echo $cars[0]; //Volvo
2、创建关联数组
$age = array("Bill"=>"60","Steve"=>"56","Mark"=>"31");
echo $age[Bill]; //60
3、创建多维数组
$cars = array
(
array("Volvo",100,96),
array("BMW",60,59),
array("Toyota",110,100)
);
4、数组长度
$cars = array("Volvo","BMW","SAAB");
echo count($cars);//3
echo sizeof($cars);//3
//js中
var arr = ["Volvo","BMW","SAAB"];
console.log(arr.length); //3
四、递归调用
$a = 0;test($a);function test($a){$a++;if ($a<3) { test($a); }echo $a."<br/>";}
//输出3,2,1 。js中情况一样
1、不等同于下面的写法,因为上面是函数里面调用函数,所以存在变量作用域的问题
function test($a){$a++; //1if ($a<3) { $a++; //2if ($a<3) { $a++; //3if ($a<3) {//这里就不执行了}echo $a."<br/>";}echo $a."<br/>";}echo $a."<br/>";}
//输出3,3,3 。js中情况一样
2、等同于下面这种写法,可以理解为调用了一个和他自己一样的函数
function test($a){$a++; //1if ($a<3) {test($a); function test($a){$a++; //2if ($a<3) { test($a); function test($a){$a++; //3if ($a<3) { //这里就不执行了}console.log($a);} }console.log($a);} }console.log($a);}
//js中输出3,2,1 。php中输出3,解释如下例
function test(){console.log(1);test(); function test(){ console.log(2);} }
//上例在js中里面的test()调用的是同级的test方法,所以输出1,2
function test(){echo 1;test(); function test(){echo 2;}}
//在php中这样是调用的外层的test方法,死循环function test(){echo 1; function test(){echo 2;}test();}
//这样只输出1五、增加一个快速排序的代码
//快速排序var arr = [2,3,50,32,41,33,45,77,87,35,36,66,54,55,1,93,4,55,3,234,432];var quickSort = function(arr) { if (arr.length <= 1) { return arr; } var pivotIndex = Math.floor(arr.length / 2); var pivot = arr.splice(pivotIndex, 1)[0]; var left = []; var right = []; for (var i = 0; i < arr.length; i++){ if (arr[i] < pivot) { left.push(arr[i]); } else { right.push(arr[i]); } } return quickSort(left).concat([pivot], quickSort(right));};console.log(quickSort(arr));
六、页面跳转及中文字符输出
页面跳转
js中---》window.location.href="http://www.baidu.com";php---》header("location:http://www.baidu.com");
如果开启缓冲,header("location:http://www.baidu.com") 可以放在任意地方执行,否则只能放在所有输出之前,建议放在第一行,最好不要开启缓冲区
开启缓冲--》ob_start();
输出中文字符:
在html中防止出现乱码,在头部添加 <metacharset=utf-8">;
在php中 header("Content-Type:text/html;charset=utf-8"); //设置页面编码要和文件编码一致
2 0
- PHP和js数组、字符串、函数不同点基础总结
- php基础函数数组函数字符串函数
- php字符串函数和数组函数
- PHP数组函数和字符串函数
- php基础 常用字符串函数总结
- php 字符串和数组相互转换函数
- //总结:数组,字典,集合的相同点和不同点
- php基础系列----4数组和函数
- 数组、字符串和函数的总结
- 基础备忘:字符数组、字符串和字符串处理基本函数
- php 常用函数总结(数组,字符串,时间,文件操作)
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- PHP数组函数总结
- PHP数组函数总结
- php数组函数总结
- php数组函数总结
- PHP数组函数总结
- 自定义view(二)五子棋
- 字符
- 安卓Handler入门学习
- 新浪sae上php部署 应用配置及日志系统
- jq删除元素操作
- PHP和js数组、字符串、函数不同点基础总结
- JAVA注解应用
- 医疗常用信息化系统介绍
- UI设计 Android开发尺寸标注
- 博客链接集会
- DBGrid 使用大全
- 堆排序
- 特征离散、归一、交叉解释或实现
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons dfs(树)