UNIX 时间戳转换为日期使用函数:FROM_UNIXTIME()
select FROM_UNIXTIME(1543452634);
日期转换为 UNIX 时间戳使用函数:UNIX_TIMESTAMP()
select UNIX_TIMESTAMP('2015-11-11 12:23:23');
中间变量(临时变量)
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
在 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
一球从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
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 语言解决鸡兔同笼问题,设已知总头数为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
基本思想:在要排序的一组数中,选出最小的一个数(要遍历一下)与第一个位置的数交换;
然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。
提示:在查找最小元素时,一定要记录下标的位置
<?php
function select(&$arr)
{
for($i=0; $i<count($arr)-1; ++$i)
{
//假设下标为$i的元素就是最小的数
$min = $arr[$i];
$minIndex = $i;
//取得最小的元素及其下标
for($j=$i+1; $j<count($arr); ++$j)
{
if($min>$arr[$j])
{
$min = $arr[$j];
$minIndex = $j;
}
}
// 最后交换
$temp = $arr[$i];
$arr[$i] = $arr[$minIndex];
$arr[$minIndex] = $temp;
}
return $arr;
};
$arr = array(12,5,33,78,96,16,8,57,62);
$res = select($arr);
print_r($res);
结果:Array ( [0] => 5 [1] => 8 [2] => 12 [3] => 16 [4] => 33 [5] => 57 [6] => 62 [7] => 78 [8] => 96 )