对url和json的了解

原创 2018-11-05 16:08:38 389
摘要:<?php     //进行url编码(在特殊字符前加上%,防止服务器解析出现歧义)     $url = urlencod('http://www.php.cn');     echo $url;   &n
<?php
    //进行url编码(在特殊字符前加上%,防止服务器解析出现歧义)
    $url = urlencod('http://www.php.cn');
    echo $url;
    //在代码中使用,使用urldecode进行解码还原就可以正常使用了
    echo '<a href="'.urldecode($url).'">php中文网</a><hr>';
    
    //json函数 1.必须是utf8编码,2.不能处理资源类型: resource
    // 1. json_encode($var),返回json字符串,失败返回false
    $name = '杨过';
    echo json_encode($name);
    $info=['name'=>'wangchu','age'=>'20','sex'=>'male'];
    echo json_encode($info);
    
    $obj = new stdClass();
    $obj->name='小龙女';
    $obj->age=21;
    $obj->sex='female';
        echo json_encode($obj);
        
        //2.json_decode($json_str,true): 默认返回对象,加true,返回数组
        $json='{"name":wangchu,"age":20,"sex":"male"}';
        //默认返回对象类型的变量
        $res = json_decode();
        echo '<pre>'.var_export($res).'<br>';
        echo $obj->name;
        
        //添加第二个参数:true,则返回的是array数组类型的变量
        $res = json_decode($json,true);
        echo '<pre>'.var_export($res,true).'<br>';
        echo $res['age'];
?>

 一、url与路径相关的函数

    因为url与文件路径都是由字符串组成,所以也放在了字符串中学习

1.urlencode($url):url编码在特殊字符前加上%,防止服务器解析出现歧义

//2.生成动态查询字符串(参数由数组提供): cate_id=3&art_id=10


 二、json 相关函数

  二点约定:1.必须是utf8编码,2.不能处理资源类型: resource

  1.json_encode():将数据转为json字符串

  2.json_decode():将json字符串进行解码还原为变量

1>. json_encode($var),返回json字符串,失败返回false

2>.json_decode($json_str,true): 默认返回对象,加true,返回数组


批改老师:韦小宝批改时间:2018-11-05 16:13:09
老师总结:完美!写的很完整!总结写的也很不错!加油!

发布手记

热门词条