PHP code example of ruanjiayou / utils-php

1. Go to this page and download the library: Download ruanjiayou/utils-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ruanjiayou / utils-php example snippets


// 取对象属性,属性不存在报错: 取消提示 $var['name']
error_reporting(E_ALL^E_NOTICE);
// 变量类型
gettype($var);// integer string double boolean NULL float array object
// 判断变量是否是函数
bool function_exists();
//魔术变量,获取当前文件的绝对路径
__FILE__
//魔术变量,获取当前脚本的目录
__DIR__

$_SERVER
//返回当前工作目录
getcwd();

/**
 * 1.下载thinkphp源码包,并解压到项目目录
 * 2.下载phpStudy
 *   其他选项菜单中,站点域名管理中添加 域名和端口,保存
 *   其他选项菜单中,打开hosts 添加记录 127.0.0.1       banyou.jiayou.com
 * 3.浏览器中输入 banyou.jiayou.com --> Hello World
 * 4.git新建仓库
 * 5.克隆仓库,并把刚解压的源码放进来
 * 6.去掉对thinkphp和vendor的 ignore
 * 7.测试动态路由 看云手册: 通常是在应用的路由配置文件application/route.php进行注册
 * 8.错误: No input file specified --> public中的.htaccess RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]改为 RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
 * 9.application下,复制utils文件夹,新建routes文件夹,application下面的route.php内容改为
 * 
 *   

class test {
  const lang = 'zh-cn';
  static $message = 'Hello World';
  function getLang() {
    return self::lang;
  }
  static function() {
    return self::$message;
  }
}
/* PHP类属性与类静态变量的访问
 * Created on 2016-7-13
 */
class test
{
 const constvar='hello world';
 static $staticvar='hello world';
 function getStaticvar(){
   return self::$staticvar;
 }
}
$obj=new test();
echo test::constvar; //输出'hello world'
echo @test::staticvar; //出错,staticvar 前必须加$才能访问,这是容易和类常量(per-class常量)容易混淆的地方之一
echo test::$staticvar; //输出'hello world'
$str='test';
//echo $str::$staticvar; //出错,类名在这不能用变量动态化
//echo $str::constvar; //出错原因同上
//在类名称存在一个变量中处于不确定(动态)状态时,只能以以下方式访问类变量
$obj2=new $str();
echo $obj2->getStaticvar();


//分割字符串: 
explode($separator, $str);
//

//数组长度
count($arr);
//循环数组
foreach($arr as $item) {

}
//返回数组中所有的键名。
array_keys();
//把数组中的每个值发送到用户自定义函数,返回新的值
array_map();
//区分数组还是对象
gettype();//没用

// http://www.w3school.com.cn/php/php_ref_filesystem.asp
// post
$_FILES['image'];
// put
get_file_content('php://input', 'r');
// 原生处理上传文件
$filepath = ROOT_PATH.'public/images/'.$_FILES["image"]["name"];
move_uploaded_file($_FILES["image"]["tmp_name"], $filepath);