首页 > phper

PHP面试常出现的小算法总结

 

<?php
  //打印一个三角形
  for($i=0;$i<=3;$i++)
  {
  for($j=0;$j<=3-$i;$j++){
  echo ‘&nbsp;’;
  }
  for($k=0;$k<=2*$i;$k++)
  {
  echo “*”;
  }
  echo ‘<br>’;
  }
  ?>
  <?php
  //杨辉三角
  for($i=0;$i<6;$i++)
  {
  //第一个和最后一个都为1
  $a[$i][0]=1;
  $a[$i][$i]=1;
  }
  for($i=2;$i<6;$i++){
  for($j=1;$j<$i;$j++)
  {
  $a[$i][$j]=$a[$i-1][$i-1]+$a[$i-1][$j];
  }
  }
  for($i=0;$i<6;$i++)
  {
  for($j=0;$j<=$i;$j++){
  echo $a[$i][$j].’&nbsp;’;
  }
  echo ‘<br/>’;
  }
  ?>
  <?php
  //合并多个数组
  function t(){
  $c=func_num_args()-1;//返回传递给函数的参数个数
  $a=func_get_args();//返回一个数组,包括函数的参数列表
  for($i=0;$i<=$c;$i++){
  if(is_array($a[$i])){
  for($j=0;$j<count($a[$i]);$j++){
  $r[]=$a[$i][$j];
  }
  }else{
  die(‘Not a array’);
  }
  }
  return $r;
  }
  print_r(t(range(1,4),range(1,4),range(1,4)));
  ?>
  <?php
  //求牛
  function cow($num,$y)
  {
  for($j=1;$j<$y;$j++)
  {
  if($j>=4 && $j<15)
  {
  $num++;
  cow($num,$y-$j);
  }
  if($j==20)
  $num–;
  }
  return $num;
  }
  echo cow(2,2);
  ?>
  <?php
  //顺序查找(数组里查找某个元素)
  function seq_sch($array,$n,$k)//在某个位置之前查找某元素(不包括指定位置)
  {
  $array[$n]=$k;
  for($i=0;$i<$n;$i++)
  {
  if($array[$i]==$k)
  break;
  }
  if($i<$n)
  return $i;
  else
  return -1;
  }
  $array=array(‘a’,'b’,'c’);
  echo seq_sch($array,2,’b');
  ?>
  <?php
  function show($i)
  {
  if($i==1)
  return 1;
  else
  return $i*show($i-1);
  }
  echo “<br>”;
  echo show(3);
  ?>
  <?php
  //裴波那挈数列
  function b($n)
  {
  if($n<2)
  return $n;
  else
  return b($n-1)+show($n-2);
  }
  echo b(2);
  ?>

阅读全文

几个简单的php数据文件缓存类

1.对于一般的变量,把该变量变成php语言的格式,写到文件中,用时只要include这个文件就相当于加载了cache了;
2.对于array型的变量,把array转化为php语言定义array的字符串,写到文件中,用时也只要include就相当于加载了cache了;
3.缓存cache时间上的控制,通过获取缓存文件的创建时间和现在的时间进行对比,如果没有到更新时间,直接读取缓存,如果到了更新时间,查询数据库,

阅读全文

php使用empty()函数要注意事项

从表面上看,很容易误解empty()函数是判断字符串是否为空的函数,其实并不是,我也因此吃了很多亏。

empty()函数是用来测试变量是否已经配置。若变量已存在、非空字符串或者非零,则返回 false 值;反之返回 true值。所以,当字符串的值为0时,也返回true,就是执行empty内部的语句。这就是陷阱。

阅读全文

一个简单php和mysql数据分页程序


<?php
// Adam's Custom PHP MySQL Pagination Tutorial and Script
// You have to put your mysql connection data and alter the SQL queries(both queries)
// This script is in tutorial form and is accompanied by the following video:
mysql_connect("DB_Host_Here","DB_Username_Here","DB_Password_Here") or die (mysql_error());
mysql_select_db("DB_Name_Here") or die (mysql_error());
//////////////  QUERY THE MEMBER DATA INITIALLY LIKE YOU NORMALLY WOULD
$sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC");
//////////////////////////////////// Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////
$nr = mysql_num_rows($sql); // Get total of Num rows from the database query
if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
    //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
} else { // If the pn URL variable is not present force it to be value of page number 1
    $pn = 1;
}
//This is where we set how many database items to show on each page
$itemsPerPage = 10;
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
    $pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
    $pn = $lastPage; // force it to be $lastpage's value
}
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
$sql2 = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC $limit");
//////////////////////////////// END Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
    // This shows the user what page they are on, and the total number of pages
    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';
    // If we are not on page 1 we can place the Back button
    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    }
    // Lay in the clickable numbers display here between the Back and Next links
    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
    // If we are not on the very last page we can place the Next button
    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
    }
}
///////////////////////////////////// END Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////////////
// Build the Output Section Here
$outputList = '';
while($row = mysql_fetch_array($sql2)){

阅读全文