mysql8.0 ubuntu 安装

mysql8.0目前是mysql要发布的下一个大版本,但是还没有对外发布,玩玩可以,请不要用在生产环境。

wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-server_8.0.1-dmr-1ubuntu16.10_amd64.deb-bundle.tar
tar -xvf mysql-server_8.0.1-dmr-1ubuntu16.10_amd64.deb-bundle.tar
sudo dpkg -i *.deb

如果报错,没有组织好对应的依赖关系,执行这条命令

sudo apt-get -f install
mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 8.0.1-dmr MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

PHP类型约束

从php5开始function的参数可以指定为对象,接口,数组,callalbe(从php5.4开始)不过如果使用 null作为参数的默认值,那么在调用函数的时候依然可以使用 null作为实参。如果一个类或接口指定了类型约束,则其所有的子类或实现也都如此。约束类型不可以用用在stirng int trait类型。

函数调用的参数与给定的约束不一致会抛出致命异常。

class demo
{
    public function array_demo(array $array)
    {
        print_r($array);
    }

    public function obj_demo(demoClass $obj = null) 
    {
        print_r($obj);
    }

    public function interface_demo(demoInterface $obj)
    {
        $obj->eat();
    }

    public function callable_demo(callable $callback, $data)
    {
        call_user_func($callback, $data);
    }
}

 

mysql 从csv导入 数据到表

mysql从csv导入数据到指定的表有如下几个要求:

1、主要语句:LOAD DATA INFILE file.csv

2、csv里面的列数要和表里面的一一对应

3、csv文件必须是文件的绝对路径

4、删除不需要的数据行

例子:

mysql> LOAD DATA INFILE /tmp/data.csv INTO TABLE users FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

FIELDS TERMINATED BY:列的分隔符是逗号

ENCLOSED BY:每一个都被双引号包裹

LINES TERMINATED BY :每一行用换行分割

 

高逼格的函数printf sprintf的使用

对于大多数php选手来说只需要使用print(),echo()这两个函数来显示文本和变量就足够用了,但是高级的选手就经常使用printf(),sprintf()函数,这个函数可以显示文本,还可以设置输出的格式。

printf(string format[, mixed arguments]);

format是由直接文本内容和特殊格式参数组成,特殊格式参数由“%”开始,可以多种组合。

“+”可以在数字前面添加个加号。

常用的字符类型说明:

b:二进制数

c:ASCII值的字符

d:带符号十进制数

u:无符号十进制

f:浮点数

o:八进制数

s:字符串

x:十六进制

printf("b: %b,c:%c,d:%d,f:%f", 80,80,1,1);
//输出:b:1010000,c:P,d:1,f:1.000000;
printf("the %s is %0.2f", 'number', 100);
//输出the number is 100.00

sprintf和printf的用法几乎一样,唯一的区别是sprintf不直接输出而是返回组装好的字符串,这使得在组长sql语句的时候将非常好用

$conn = mysqli_connect('localhost', 'root', 'root');
$sql = sprintf("insert into users (name, email, age, avg_score) vluses(%s, %s, %d, $0.2f)", $name, $email, $age, $avg_score);
mysqli_query($conn, $sql);

和printf()一样好用的还有vprintf(),和vsprintf()不同的是这两个函数只接收两个参数,格式和一个数组,要注意数组的顺序

vprintf("%0.2f score is %s", [100, 'very good']);

vsprintf("%0.2f score is %s", [100, 'very good']);

 

说一说按值传递和按引用传递

在默认情况下,函数是按值传递的,也就是传递的是变量的值,而不是变量的本身,也就说函数使用的是变量的一份副本,这种传递的结果就是不会影响变量本身的值。

除了默认行为我们还可以使用引用传递,就是传递变量本身,而不是按值传递,这样有两个好处,第一个是可以改变函数外部变量本身的值,而不需要定义全局变量。第二个好处是如果需要引用传递的数据比较大的场景,按照引用传递则php不在需要构建一份数据的副本,进而提高运行效率。

引用传递需要在变量前面添加一个 “&”符号:

$num = 1;

function func(&$num) {
    $num++;
}

func($num);  //2

//php5.4以后不在支持下面这种写法


function func($num) {
    $num++;
}

func(&$num);

 

php 二维数组排序

<?php

$array = [
    ['key1' => 10, 'key2' => 'wang'],
    ['key1' => 2, 'key2' => 'WANG'],
    ['key1' => 30, 'key2' => 'king'],
    ['key1' => 34, 'key2' => 'KING'],
    ['key1' => 301, 'key2' => 'kING'],
    ['key1' => 20, 'key2' => 'King'],
    ['key1' => 400, 'key2' => 'Wang'],
    ['key1' => 400, 'key2' => 'ABCDEFG'],
    ['key1' => 400, 'key2' => 'ABCDEFg'],
    ['key1' => 400, 'key2' => 'AbcDEFG'],
    ['key1' => 400, 'key2' => 'AbcdEFG'],
    ['key1' => 400, 'key2' => 'Bacdefg'],
];
¬
function asc_sort($a, $b) {
    if($a['key1'] > $b['key1']) {
        return true;¬
    } else if($a['key1'] < $b['key1']) {
        return false;
    } else {
        return 0;
    }
}



function asc_name_sort($a, $b) {
    return strcmp($a['key2'], $b['key2']);
}


//usort($array, 'asc_sort');
usort($array, 'asc_name_sort');

var_dump($array);

 

无符号8位? 无符号32位?有符号8位?

简单来说位指的是二进制的位数

无符号就是全都是整数0以上的数字,有符号就是包括负数带有负号就叫有符号

无符号 8 位整型 (0 到 255)
无符号 16 位整型 (0 到 65535)
无符号 32 位整型 (0 到 4294967295)
无符号 64 位整型 (0 到 18446744073709551615)
有符号 8 位整型 (-128 到 127)
有符号 16 位整型 (-32768 到 32767)
有符号 32 位整型 (-2147483648 到 2147483647)
有符号 64 位整型 (-9223372036854775808 到 9223372036854775807)

 

linux 实用和好玩的命令

执行上一条命令

!!

用root权限执行上一条命令

sudo !!

执行历史命令的第上几条

!+数字

快速在当前目录下建立一个http服务器,端口默认为8000 可以用来传输文件

python -m SimpleHTTPServer

格式化json数据

echo '{"a": "aa", "b": "bb", "c": "cc"}' | python -m json.tool

{
 "a": "aa",
 "b": "bb",
 "c": "cc"
}

删除文件中的空行

cat a.txt | sed -e '/^$/d'

名言警句,或者好玩的段子,还有可能是一首诗

fortune

黑客帝国

cmatrix

 

 

laravel zttp 用法

$response = Zttp::withHeaders(['Fancy' => 'Pants'])->post($url, [
    'foo' => 'bar',
    'baz' => 'qux',
]);

$response->json();

 

$response = Zttp::asFormParams()->post($url, [
    'foo' => 'bar',
    'baz' => 'qux',
]);

 

$response = Zttp::patch($this->url('/patch'), [
    'foo' => 'bar',
    'baz' => 'qux',
]);

 

$response = Zttp::delete($this->url('/delete'), [
    'foo' => 'bar',
    'baz' => 'qux',
]);

 

$response = Zttp::accept('banana/sandwich')->post($url);

 

$response = Zttp::withoutRedirecting()->get($url);

 

zttp tests.php file 几个简单使用案例:

<?php
use Zttp\Zttp;
use PHPUnit\Framework\TestCase;
class ZttpTest extends TestCase
{
    public static function setUpBeforeClass()
    {
        ZttpServer::start();
    }
    function url($url)
    {
        return vsprintf('%s/%s', [
            'http://localhost:' . getenv('TEST_SERVER_PORT'),
            ltrim($url, '/'),
        ]);
    }
    /** @test */
    function query_parameters_can_be_passed_as_an_array()
    {
        $response = Zttp::get($this->url('/get'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'query' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function query_parameters_in_urls_are_respected()
    {
        $response = Zttp::get($this->url('/get?foo=bar&baz=qux'));
        $this->assertArraySubset([
            'query' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function query_parameters_in_urls_can_be_combined_with_array_parameters()
    {
        $response = Zttp::get($this->url('/get?foo=bar'), [
            'baz' => 'qux'
        ]);
        $this->assertArraySubset([
            'query' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function post_content_is_json_by_default()
    {
        $response = Zttp::post($this->url('/post'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'headers' => [
                'content-type' => ['application/json'],
            ],
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function post_content_can_be_sent_as_form_params()
    {
        $response = Zttp::asFormParams()->post($this->url('/post'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'headers' => [
                'content-type' => ['application/x-www-form-urlencoded'],
            ],
            'form_params' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function post_content_can_be_sent_as_json_explicitly()
    {
        $response = Zttp::asJson()->post($this->url('/post'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'headers' => [
                'content-type' => ['application/json'],
            ],
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function get_with_additional_headers()
    {
        $response = Zttp::withHeaders(['Custom' => 'Header'])->get($this->url('/get'));
        $this->assertArraySubset([
            'headers' => [
                'custom' => ['Header'],
            ],
        ], $response->json());
    }
    /** @test */
    function post_with_additional_headers()
    {
        $response = Zttp::withHeaders(['Custom' => 'Header'])->post($this->url('/post'));
        $this->assertArraySubset([
            'headers' => [
                'custom' => ['Header'],
            ],
        ], $response->json());
    }
    /** @test */
    function the_accept_header_can_be_set_via_shortcut()
    {
        $response = Zttp::accept('banana/sandwich')->post($this->url('/post'));
        $this->assertArraySubset([
            'headers' => [
                'accept' => ['banana/sandwich'],
            ],
        ], $response->json());
    }
    /** @test */
    function exceptions_are_not_thrown_for_40x_responses()
    {
        $response = Zttp::withHeaders(['Z-Status' => 418])->get($this->url('/get'));
        $this->assertEquals(418, $response->status());
    }
    /** @test */
    function exceptions_are_not_thrown_for_50x_responses()
    {
        $response = Zttp::withHeaders(['Z-Status' => 508])->get($this->url('/get'));
        $this->assertEquals(508, $response->status());
    }
    /** @test */
    function redirects_are_followed_by_default()
    {
        $response = Zttp::get($this->url('/redirect'));
        $this->assertEquals(200, $response->status());
        $this->assertEquals('Redirected!', $response->body());
    }
    /** @test */
    function redirects_can_be_disabled()
    {
        $response = Zttp::withoutRedirecting()->get($this->url('/redirect'));
        $this->assertEquals(302, $response->status());
        $this->assertEquals($this->url('/redirected'), $response->header('Location'));
    }
    /** @test */
    function patch_requests_are_supported()
    {
        $response = Zttp::patch($this->url('/patch'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function put_requests_are_supported()
    {
        $response = Zttp::put($this->url('/put'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function delete_requests_are_supported()
    {
        $response = Zttp::delete($this->url('/delete'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function query_parameters_are_respected_in_post_requests()
    {
        $response = Zttp::post($this->url('/post?banana=sandwich'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'query' => [
                'banana' => 'sandwich',
            ],
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function query_parameters_are_respected_in_put_requests()
    {
        $response = Zttp::put($this->url('/put?banana=sandwich'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'query' => [
                'banana' => 'sandwich',
            ],
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function query_parameters_are_respected_in_patch_requests()
    {
        $response = Zttp::patch($this->url('/patch?banana=sandwich'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'query' => [
                'banana' => 'sandwich',
            ],
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function query_parameters_are_respected_in_delete_requests()
    {
        $response = Zttp::delete($this->url('/delete?banana=sandwich'), [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);
        $this->assertArraySubset([
            'query' => [
                'banana' => 'sandwich',
            ],
            'json' => [
                'foo' => 'bar',
                'baz' => 'qux',
            ]
        ], $response->json());
    }
    /** @test */
    function can_retrieve_the_raw_response_body()
    {
        $response = Zttp::get($this->url('/simple-response'));
        $this->assertEquals("A simple string response", $response->body());
    }
    /** @test */
    function can_retrieve_response_header_values()
    {
        $response = Zttp::get($this->url('/get'));
        $this->assertEquals('application/json', $response->header('Content-Type'));
    }
    /** @test */
    function can_check_if_a_response_is_success()
    {
        $response = Zttp::withHeaders(['Z-Status' => 200])->get($this->url('/get'));
        $this->assertTrue($response->isSuccess());
        $this->assertFalse($response->isRedirect());
        $this->assertFalse($response->isClientError());
        $this->assertFalse($response->isServerError());
    }
    /** @test */
    function can_check_if_a_response_is_redirect()
    {
        $response = Zttp::withHeaders(['Z-Status' => 302])->get($this->url('/get'));
        $this->assertTrue($response->isRedirect());
        $this->assertFalse($response->isSuccess());
        $this->assertFalse($response->isClientError());
        $this->assertFalse($response->isServerError());
    }
    /** @test */
    function can_check_if_a_response_is_client_error()
    {
        $response = Zttp::withHeaders(['Z-Status' => 404])->get($this->url('/get'));
        $this->assertTrue($response->isClientError());
        $this->assertFalse($response->isSuccess());
        $this->assertFalse($response->isRedirect());
        $this->assertFalse($response->isServerError());
    }
    /** @test */
    function can_check_if_a_response_is_server_error()
    {
        $response = Zttp::withHeaders(['Z-Status' => 508])->get($this->url('/get'));
        $this->assertTrue($response->isServerError());
        $this->assertFalse($response->isSuccess());
        $this->assertFalse($response->isRedirect());
        $this->assertFalse($response->isClientError());
    }
}
class ZttpServer
{
    static function start()
    {
        $pid = exec('php -S ' . 'localhost:' . getenv('TEST_SERVER_PORT') . ' -t ./tests/server/public > /dev/null 2>&1 & echo $!');
        while (@file_get_contents('http://localhost:' . getenv('TEST_SERVER_PORT') . '/get') === false) {
            usleep(1000);
        }
        register_shutdown_function(function () use ($pid) {
            exec('kill ' . $pid);
        });
    }
}

要求:PHP>7.0 PHPUNIT>=6.0