PHP 分布式中 Redis 实现 Session 共享

找到配置文件php.ini,修改为下面内容,保存并重启服务

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

直接在代码中加入以下内容:

ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://127.0.0.1:6379");

测试:

<?php
//ini_set("session.save_handler", "redis");
//ini_set("session.save_path", "tcp://127.0.0.1:6379");

session_start();

//存入session
$_SESSION['class'] = array('name' => 'toefl', 'num' => 8);

//连接redis
$redis = new redis();
$redis->connect('127.0.0.1', 6379);

//检查session_id
echo 'session_id:' . session_id() . '<br/>';

//redis存入的session(redis用session_id作为key,以string的形式存储)
echo 'redis_session:' . $redis->get('PHPREDIS_SESSION:' . session_id()) . '<br/>';

//php获取session值
echo 'php_session:' . json_encode($_SESSION['class']);

 

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

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

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

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

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

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

lnmp restart

一球从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

使用 PHP 描述选择排序算法

基本思想:在要排序的一组数中,选出最小的一个数(要遍历一下)与第一个位置的数交换;
然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

提示:在查找最小元素时,一定要记录下标的位置

<?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 )

PHP读取大文件末尾N行的高效方法

小文件几兆以内大小,都可以通过 file() 函数,将文件按行读入数组,在用 array_pop 取得最后一行,就可以了。
但是对于很大的文本文件来说,机器内存不够大,或者 php 本身 memory_limit 有限制,这个办法就不适用了,即使强行不限制,效率也是非常低的。

没有办法了吗?当然有,不过没有现成的函数了,需要自己动手了。

这里需要用到文件指针,通俗的讲吧,PHP 中通过 fopen 打开一个文件,这时候还没有读取文件,这时候指向的是文件开头,指针位置也就是 0,当你通过 fgets 或者 fgetc 从文件中读取内容的时候,你读多少,指针也相应往前进多少,这也是

while(!feof($fp)){
$data.=fgets($fp,4096);
}

得以实现的原理,即 fgets 是从当前指针位置向后读取指定长度的字符串,直到遇见换行符为止。

那么可不可以控制指针的位置到倒数第 N 行位置呢?很遗憾,没有,但是可以将指针直接移动到末尾,并倒退 N 个位置,通过 fseek() 函数。

我们先将指针移动到末尾,并向后倒退 2 个位置,通过 fgetc 读取一个字符,判断这个字符是不是 "\n" 也就是换行符,如果不是换行符,那么继续倒退一个位置再次判断,直到我们倒退到上一行的结尾换行符为止,直接使用 fgets 将一整行都取出来即可。这里面用到两个 while 循环,外层循环控制需要取得的行数,内层循环控制 fseek 动作。

函数如下:

/**
 * 取文件最后$n行
 * @param string $filename 文件路径
 * @param int $n 最后几行
 * @return mixed false表示有错误,成功则返回字符串
 */
function FileLastLines($filename,$n){
    if(!$fp=fopen($filename,'r')){
        echo "打开文件失败,请检查文件路径是否正确,路径和文件名不要包含中文";
        return false;
    }
    $pos=-2;
    $eof="";
    $str="";
    while($n>0){
        while($eof!="\n"){
            if(!fseek($fp,$pos,SEEK_END)){
                $eof=fgetc($fp);
                $pos--;
            }else{
                break;
            }
        }
        $str.=fgets($fp);
        $eof="";
        $n--;
    }
    return $str;
}
echo nl2br(FileLastLines('sss.txt',4));

via: http://www.thinkphp.cn/topic/4127.html

约瑟夫环

是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。

要求输入:n,k,m

输出:退出顺序

<?php
//$n 人数 $k 开始位置  $m 要数得数字
function array_f($n,$k,$m){
    for($i=1; $i <= $n ; $i++){
        $oldArr[] = $i;
    }    
    for ($i=0;$i<$k-1;$i++){
        $num = array_shift($oldArr);
        $oldArr[] = $num;
    }
    for($i=0;$i<$n;$i++){
        for($j=0;$j<$m-1;$j++){
            $num = array_shift($oldArr);
            $oldArr[] = $num;
        }
        $newArr[] = array_shift($oldArr);
    }
    var_dump($newArr);
}
array_f(6,2,3);