您的位置:首页 > 编程语言 > PHP开发

PHP和js数组、字符串、函数不同点基础总结

2017-03-06 18:46 771 查看

一、在js中,函数内部可以访问到函数外部定义的变量,而在php中不同

$test = 123;

abc(); //这里什么都不输出,因为访问不到$test变量
function abc(){

echo($test);

}

可以通过下面的方法在函数内部访问函数外部的变量

//1、传参进去
$test = 123;

abc($test); //123
function abc($test){

echo($test);

}

//2、定义全局变量
$test = 123;

abc(); //123
function abc(){

global $test;

echo($test);

}

//3、同上
global $test;
$test = 123;

abc(); //123
function 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++;   //1
if ($a<3) {
$a++;   //2
if ($a<3) {
$a++;   //3
if ($a<3) {
//这里就不执行了
}
echo $a."<br/>";
}
echo $a."<br/>";
}
echo $a."<br/>";
}

//输出3,3,3 。js中情况一样

2、等同于下面这种写法,可以理解为调用了一个和他自己一样的函数

function test($a){
$a++;   //1
if ($a<3) {

test($a);
function test($a){
$a++;   //2
if ($a<3) {

test($a);
function test($a){
$a++;   //3
if ($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中防止出现乱码,在头部添加   <meta
charset=utf-8">;

在php中    header("Content-Type:text/html;charset=utf-8");  //设置页面编码要和文件编码一致
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php