JS 两个变量互换值

中间变量(临时变量)

var strA = "a"
var strB = "b"
var strC
strC = strB
strB = strA
strA = strC

若变量为数字,可使用加减法

//加法
var a = 2
var b = 3
a += b
b = a - b
a = a - b
//减法
a += b
b = a - b
a -= b

对象和数组

var a = "1" , b = "2"
对象的方法:
先把a变成一个对象,即
a={a:b,b:a}
b = a.a
a = a.b
数组的方法:
数组其实和对象的思想差不多
a = [a,b]
b = a[0]
a = b[1]

万能法(运用运算符优先级)

var a = "1", b= "code"
a = [b, b=a][0]
console.log(a,b)

ES6的解构赋值

什么是解构赋值?
解构赋值允许你使用类似数组或对象字面量的语法将数组和对象的属性赋给各种变量。这种赋值语法极度简洁,同时还比传统的属性访问方法更为清晰。

数组与迭代器的解构
语法:[ variable1, variable2, ..., variableN ] = array;
这将为variable1到variableN的变量赋予数组中相应元素项的值

可以去看看解构的赋值:http://es6.ruanyifeng.com/#docs/destructuring

let a = "one",b = "two";
[a, b] = [b, a];
console.log(a, b);//two one

利用try catch交换

var a=1,b=2;
a = (function () {;
    try{
        return b
    } finally {
        b = a
    }
})();
或字符串
var a = "aaa",
    b = "bbb";
a = (function () {;
    try {
        return b
    } finally {
        b = a
    }
})();
console.log(a, b);

异或运算(针对数字)

var a = 1; // 二进制:0001
var b = 2; // 二进制:0010
a = a ^ b; // 计算结果:a = 0011, b = 0010
b = a ^ b; // 计算结果:a = 0011, b = 0001
a = a ^ b; // 计算结果:a = 0010, b = 0001
自己可以动手试一下

例子:
var a = 0;
var b = 1;
a = (b = (a ^= b) ^ b) ^ a;
console.log(a,b)

例子:
var a = 0; // 二进制:0001
var b = 1; // 二进制:0010
a ^=b;
b ^=a;
a ^=b;
console.log(a,b)

数组的两个值的交换

var arr = [item0,item1,...,itemN];
//最初使用这段代码来交换第0个和第K(k<N)个元素
arr[0] = arr.splice(k, 1, arr[0])[0];

var arr = [1,2,3,"aaa","bbb","ccc"];
arr[0] = arr.splice(3, 1, arr[0])[0];
console.log(arr.toString())//aaa,2,3,1,bbb,ccc

via: http://www.jianshu.com/p/fb863e73862f

Laravel 的多数据库连接

在 app/config/database.php  中可以设置

<?php
return array(
    'default' => 'mysql',
    'connections' => array(
        # Our primary database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'host1',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Our secondary database connection
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => 'host2',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

在 controller 中指定数据库

Schema::connection('mysql2')->create('some_table', function($table)
{
    $table->increments('id'):
});
$users = DB::connection('mysql2')->select(...);
<?php
class SomeModel extends Eloquent {
    protected $connection = 'mysql2';
}
<?php
class SomeController extends BaseController {
    public function someMethod()
    {
        $someModel = new SomeModel;
        $someModel->setConnection('mysql2');
        $something = $someModel->find(1);
        return $something;
    }
}

具体有时间再翻译

via: http://fideloper.com/laravel-multiple-database-connections

搬瓦工搬家记录

搬家的一点记录

yum -y upgrade
yum -y install wget
yum -y install vim
wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh lnmp
lnmp vhost add
vim /usr/local/php/etc/php.ini
:set nu
:314
:wq
cd /root/lnmp1.4
./pureftpd.sh
lnmp ftp add
lnmp restart

服务器整站远程迁移备份

cd /home
tar czf - wwwroot | ssh root@67.216.213.31 -p 28376 tar xzf - -C /home

解决 “噢,没有这个文件,请重新检查文件名,然后再试。谢谢”

编辑主题时提示:噢,没有这个文件,请重新检查文件名,然后再试。谢谢

因使用 lnmp 一键安装包所致,进行如下操作:

找到服务器上的 php.ini 的位置,可以用 phpinfo() 函数查看该文件的位置,找到后编辑该文件,

vi /usr/local/php/etc/php.ini

找到第314行或附近的disable_functions按 i 键进入编辑模式,将其值里的 scandir 这个函数去掉,记得逗号也要去掉,再esc退出编辑模式,输入:wq 保存退出,之后重启你的nginx服务和php服务,或直接重启的lnmp

lnmp restart

解决安装WordPress主题及插件需要输入FTP问题,要执行请求的操作,WordPress需要访问您网页服务器的权限。请输入您的FTP登陆凭据以继续

更改项目目录权限及用户组

chown -R www /home/wwwroot/guofeng.io/
chmod -R 775 /home/wwwroot/guofeng.io/

.user.ini文件无法直接修改,如要修或删除需要先执行:chattr -i /网站目录/.user.ini

chattr -i /home/wwwroot/guofeng.io/.user.ini
chattr +i /home/wwwroot/guofeng.io/.user.ini

一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。编写程序,输出第n次落地时,小球落下弹起共经过多少米。

一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。编写 PHP 程序,输出第 n 次落地时,小球落下弹起共经过多少米。

<?php
function rebouns($n) {
    $links = array();
    $begin = 100;
    for($i=1; $i<=$n; $i++){
        if($i==1 || $i==2){
            $links[$i] = 100;
        }else{
            $links[$i] = $links[$i-1]/2;
        }
        $sum = array_sum($links);
        echo "第".$i."次落地,经过距离为".$sum."<br />";
    }
}
rebouns(5);

输出结果为:

第1次落地,经过距离为100
第2次落地,经过距离为200
第3次落地,经过距离为250
第4次落地,经过距离为275
第5次落地,经过距离为287.5

Please compose a functio thar meets the following requirement

We are a php shop and prefer you answering in php,

But you might answer in any other languages in case you are not familiarwith it, we can read C#, Java, C++, Perl, Ruby, etc.

Requirement:

The input would be an English sentence as a string, please transforms it as described below and return a new…

The sentence would contains only alphabet(a-z and A-Z) and space, each word would be separated by exactly…

space. There would be no spaces before and after the sentence.

Please return the string with each word spelled in reverse, however, the position of the capitalization of each …

should stay the same for each word.

For example:

Input: This is an Apple on eBay

Output: sihT si na elppA no yaBe

<?php
function rev($arr) {
    $arr = explode(" ",$arr);
    foreach($arr as &$v){
        $v = strrev($v);
    }
    return implode(" ",$arr);
}
$arr = "This is an Apple on eBay";
$res = rev($arr);
print_r($res);

输出为:sihT si na elppA no yaBe

 

 

用 PHP 语言解决鸡兔同笼问题

用 PHP 语言解决鸡兔同笼问题,设已知总头数为H,总脚数为Y,求鸡兔各有多少只?

<?php
function fn($h,$y) {
    $maxC = $h;
    $minC = ceil($y/4);
    $c = $minC;
    $r = false;
    for(; $c<$maxC; $c++)
    {
        $r = $h - $c;
        if(($c*2 + $r*4) == $y) return $c;
    }
    return false;
}
$chick = fn(10,26);
if(false == $chick){
    echo "输入有误";
}else{
    $rabbit = 10 - $chick;
    echo "鸡:{$chick},兔:{$rabbit}";
}

输出为:鸡:7,兔:3