php面接問題と答え(揚)
57361 ワード
:
1. get post ?
:get HTTP url , post , .
2.session cookie ?
:session: , php (session_dir)
cookie: , , Cookie WIN Temp 。
3. ?
: (transaction) 。 , , , 。 ,
, 。 , , 。
:
1、 PHP 2006-5-10 22:21:21(2 )
:echo date('Y-m-d H:i:s', strtotime('-1 days'));
2、echo(),print(),print_r() (3 )
:echo PHP , print print_r , , ( )
print() ( int,string)
print_r() ( , )
echo
3、 HTML PHP (1 )
:Smarty,Dwoo,TinyButStrong,Template Lite,Savant,phemplate,XTemplate
5、 ?(1 )
:cvs,svn,vss;
6、 ?(3 )
:echo strrev($a);
7、 MYSQL 。(4 , )
:
1、 , , NOT NULL, ' , ', ENUM
2、 (JOIN) :
a. :DELETE FROM customerinfo WHERE customerid NOT in(SELECT customerid FROM orderinfo)
b. :SELECT FROM customerinfo WHERE customerid NOT in(SELECT customerid FROM orderinfo)
c. b :SELECT FROM customerinfo LEFT JOIN orderid customerinfo.customerid=orderinfo.customerid
WHERE orderinfo.customerid IS NULL
3、 (UNION)
a. :SELECT name FROM `nametest` UNION SELECT username FROM `nametest2`
4、 :
a. , , ,
mysql_query("BEGIN");
mysql_query("INSERT INTO customerinfo (name) VALUES ('$name1')";
mysql_query("SELECT * FROM `orderinfo` where customerid=".$id");
mysql_query("COMMIT");
5、 , :
a. SELECT , , UPDATE 。
WRITE LOCK TABLE UNLOCK TABLES ,
inventory 、
mysql_query("LOCK TABLE customerinfo READ, orderinfo WRITE");
mysql_query("SELECT customerid FROM `customerinfo` where id=".$id);
mysql_query("UPDATE `orderinfo` SET ordertitle='$title' where customerid=".$id);
mysql_query("UNLOCK TABLES");
6、 ,
a. customerinfo customerid orderinfo customerid,
customerid orderinfo
CREATE TABLE customerinfo
(
customerid INT NOT NULL,
PRIMARY KEY(customerid)
)TYPE = INNODB;
CREATE TABLE orderinfo
(
orderid INT NOT NULL,
customerid INT NOT NULL,
PRIMARY KEY(customerid,orderid),
FOREIGN KEY (customerid) REFERENCES customerinfo
(customerid) ON DELETE CASCADE
)TYPE = INNODB;
:'ON DELETE CASCADE', customerinfo order
, INNODB;
7、 :
a. :
( )->
:CREATE INDEX < > ON tablename ( )
:ALTER TABLE tablename ADD INDEX [ ] ( )
:CREATE TABLE tablename([...],INDEX[ ]( ))
( )->
:CREATE UNIQUE < > ON tablename ( )
:ALTER TABLE tablename ADD UNIQUE [ ] ( )
:CREATE TABLE tablename([...],UNIQUE[ ]( ))
( )->
, , :
CREATA TABLE tablename ([...],PRIMARY KEY[ ])
8、
a. ,
1:
SELECT * FROM order WHERE YEAR(orderDate)<2008;( )
SELECT * FROM order WHERE orderDate<"2008-01-01";( )
2:
SELECT * FROM order WHERE addtime/7<24;( )
SELECT * FROM order WHERE addtime<24*7;( )
3:
SELECT * FROM order WHERE title like "%good%";
SELECT * FROM order WHERE title>="good" and name<"good";
8、PHP ( 1 )
:PHP , PHP HTML
9、MYSQL ?, (2 )
:now(),date()
10、 。(3 )
:function GBsubstr($string, $start, $length) {
if(strlen($string)>$length){
$str=null;
$len=$start+$length;
for($i=$start;$i<$len;$i++){
if(ord(substr($string,$i,1))>0xa0){
$str.=substr($string,$i,2);
$i++;
}else{
$str.=substr($string,$i,1);
}
}
return $str.'...';
}else{
return $string;
}
}
11、 ? ?(1 )
12、 ? ?(1 )
: ,smarty
13、 (4 )
:
14、 , ?(4 )
: , , ,
, , ,
15、 PHP IP IP 1 )
: IP:echo $_SERVER[‘REMOTE_ADDR’]; : getenv('REMOTE_ADDR');
IP:echo gethostbyname("www.bolaiwu.com")
16、 include require ? , (?) ? (2 )
:require->require require, require
include->include , require ( require include )
: require ,include
17、 SESSION (1 ).
: 1: php.ini session.gc_maxlifetime 9999 apache
2:$savePath = "./session_save_dir/";
$lifeTime = * ;
session_save_path($savePath);
session_set_cookie_params($lifeTime);
session_start();
3:setcookie() and session_set_cookie_params($lifeTime);
18、 , PHP : http://www.phpres.com/index.html, ?($1 )
: 1( PHP5 ):
$readcontents = fopen("http://www.phpres.com/index.html", "rb");
$contents = stream_get_contents($readcontents);
fclose($readcontents);
echo $contents;
2:
echo file_get_contents("http://www.phpres.com/index.html");
19、 HTTP 1.0 , 401 (?); “ ” , header , (?);(2 )
: 401 ,header("Location:www.xxx.php");
12、 PHP ,heredoc , ?(1 )
:heredoc "<<<" ,
:
$str = <<<SHOW
my name is Jiang Qihui!
SHOW;
13、 asp,php,jsp (1 )
:ASP Active Server Pages, WEB ,
、 、 WEB 。ASP VB Script(Java script
) 。
PHP . C,Java Perl
, PHP , WEB .
。 ,PHP , , PHP (http://www.php.ne
t) 。 , 。
JSP Sun , ASP,PHP --
( PHP4 Zend , ).Sun Jav
a , Java Java Java Applet , , Js
p--Java Server Page。Jsp Serverlet JavaBean ,
。
HTML 、 。
JSP Servlet Java , JSP
。 ASP 、PHP、JSP , HTML
, 。 HTML Web , ASP 、PH
P、JSP 。
HTML , 。 ASP 、PHP、 JSP Web
, 。
14、 mvc (1 )
: (model), (view), (controller)
, ;
15、 SQL, :members(id,username,posts,pass,email)(2 )
:SELECT * FROM `members` ORDER BY posts DESC limit 0,10;
16. php 。 ?(2 )
: :
:
: ,php 。 , 。
, 。
17. PHP error_reporting ? (1 )
:
18. (2 )
:function checkEmail($email)
{
$pregEmail = "/([a-z0-9]*[-_/.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[/.][a-z]{2,3}([/.][a-z]{2})?/i";
return preg_match($pregEmail,$email);
}
19. , 。(2 )
:$script_name = basename(__file__); print_r($script_name);
21、JS ? ? (2 )
: : alert(),prompt(),confirm()
focus()
22、JS ? JS ?(2 )
:window.location.href,<script type="text/javascript" src="js/js_function.js"></script>
23、foo() @foo() ?(1 )
:@foo()
24、 ”myclass” ? (1 )
:class myclass{ }
25、 ”myclass” ?(1 )
:new myclass()
26、 ? (2 )
:$object = new myclass();
$newstr = $object->test;
$object->test = "info";
27、mysql_fetch_row() mysql_fetch_array ? (1 )
:mysql_fetch_row 1 ,
mysql_fetch_array , ,
28、GD ? (1 )
:gd API, GD , 。
GD 。
29、 PHP HTML 。(1 )
:echo "<a href='index.php'>aaa</a>";
30、 , ?(1 )
(a) fget() (b) file_open() (c) fopen() (d) open_file() [ c ]
31、 john users ? (1 )
(a) $users[] = ‘john’;
(b) array_add($users,’john’);
(c) array_push($users,‘john’);
(d) $users ||= ‘john’; [ a , c ]
32、 ?(1 )
$num = 10;
function multiply(){
$num = $num * 10;
}
multiply();
echo $num;
?>
:10
33、 php , “ ” (2 )
User
Name Tel Content Date
13333663366 2006-10-11
13612312331 2006-10-15
021-55665566 2006-10-15
:
$mysql_db=mysql_connect("local","root","pass");
@mysql_select_db("DB",$mysql_db);
$result = mysql_query("SELECT * FROM `user` WHERE name=' '");
while($rs = mysql_fetch_array($result)){
echo $rs["tel"].$rs["content"].$rs["date"];
}
34、 , ?(3)
class test{
function Get_test($num){
$num=md5(md5($num)."En");
return $num;
}
}
:$testnum = "123";
$object = new test();
$encrypt = $object->Get_test($testnum);
echo $encrypt;
test Get_test ,
35、 SQL : , , (4 )
User
Name Tel Content Date
13333663366 2006-10-11
13612312331 2006-10-15
021-55665566 2006-10-15
(a) ( 13254748547 2007-05-06) SQL
mysql_query("INSERT INTO `user` (name,tel,content,date) VALUES
(' ','13254748547',' ','2007-05-06')")
(b) sql
$nowDate = date("Ymd");
mysql_query("UPDATE `user` SET date='".$nowDate."' WHERE name=' '");
(c)
mysql_query("DELETE FROM `user` WHERE name=' '");
36、 (int char varchar datetime text) ; varchar char (2 )
:int ,char ,varchar ,datetime ,text
char ,varchar
38、 (1 )
$b=201;
$c=40;
$a=$b>$c?4:5;
echo $a;
?>
:4
39、 ? ?(2 )
:isset($str),empty($str);
40、 ?(1 )
:mysql_num_rows($result);
41、$arr = array('james', 'tom', 'symfony'); (1 )
:echo $array[0];
42、 41 ',' (1 )
:for($i=0;$i<count($array);$i++){ echo $array[$i].",";}
43、$a = 'abcdef'; $a (1 )
:echo $a{0} echo substr($a,0,1)
44、PHP sql server/oracle ?(1 )
:
45、 PHP5 (3 )
:public( ),private( ),protected( )
46、 php5 (2 )
:__construct , __destruct
47、 :
( ) , message (3 )
id id
title
content
category_id id
hits
:CREATE TABLE 'message'(
'id' int(10) NOT NULL auto_increment,
'title' varchar(200) default NULL,
'content' text,
'category_id' int(10) NOT NULL,
'hits' int(20),
PRIMARY KEY('id');
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
( ) : comment , (4 )
comment_id id
id id, message id
comment_content
, ,
id
SQL , 0
:SELECT message.id id,message.title title,IF(message.`hits` IS NULL,0,message.`hits`) hits,
IF(comment.`id` is NULL,0,count(*)) number FROM message LEFT JOIN
comment ON message.id=comment.id GROUP BY message.`id`;
( ) , category , (3 )
category_id int(4) not null auto_increment;
categroy_name varchar(40) not null;
,
:function categoryList()
{
$result=mysql_query("select category_id,categroy_name from category")
or die("Invalid query: " . mysql_error());
print("<select name='category' value=''>/n");
while($rowArray=mysql_fetch_array($result))
{
print("<option value='".$rowArray['category_id']."'>".$rowArray['categroy_name']."</option>/n");
}
print("</select>");
}
:
1. , , url
: http://www.sina.com.cn/abc/de/fg.php?id=1 php .php
1:
function getExt($url){
$arr = parse_url($url);
$file = basename($arr['path']);
$ext = explode(".",$file);
return $ext[1];
}
2:
function getExt($url) {
$url = basename($url);
$pos1 = strpos($url,".");
$pos2 = strpos($url,"?");
if(strstr($url,"?")){
return substr($url,$pos1 + 1,$pos2 - $pos1 - 1);
} else {
return substr($url,$pos1);
}
}
2. HTML , meta , meta
PHP , HTML meta charset big5
:
1. html , meta
2.
3. ' "
4. 'Content-Type' , 'text/html; charset=gbk'
5.
3. ,
$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
$b $a http://www.cnblogs.com/c/d ()
:function getRelativePath($a, $b) {
$returnPath = array(dirname($b));
$arrA = explode('/', $a);
$arrB = explode('/', $returnPath[0]);
for ($n = 1, $len = count($arrB); $n < $len; $n++) {
if ($arrA[$n] != $arrB[$n]) {
break;
}
}
if ($len - $n > 0) {
$returnPath = array_merge($returnPath, array_fill(1, $len - $n, '..'));
}
$returnPath = array_merge($returnPath, array_slice($arrA, $n));
return implode('/', $returnPath);
}
echo getRelativePath($a, $b);
:
1. PHP , ( ) __$_SERVER['PHP_SELF']__ ; URL __$_SERVER['HTTP_REFERER']__
2. <?php echo 8%(-2) ?> __0__。
3. HTTP 1.0 , 401 ____; “ ” , header , ____。
4. arsort __ __; error_reporting(2047) __ __。
5.PEAR ____。
6. , JS/VBS ( scrīpt ):preg_replace("/<script[^>].*?>.*?</script>/si", "newinfo", $script);
7. Apache PHP, http.conf ____ PHP , ____ Apache php PHP 。
LoadModule php5_module "c:/php/php5apache2.dll" , AddType application/x-httpd-php .php,
8. include require , ____; , __require_once||include_once__ 。
9. session , , ____。
10. , php.ini __allow_call_time_pass_reference boolean__ on.
11.SQL LEFT JOIN __ __。 tbl_user (name) (ID),tbl_score ( , ) (ID)
(score) (subject), , SQL ____。
12. PHP ,heredoc , ____。
:
13. , 。
:
function my_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !== false ) {
if ( $file != ".." && $file != "." ) {
if ( is_dir($dir . "/" . $file) ) {
$files[$file] = scandir($dir . "/" . $file);
}else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
14. 。
:
<?php
/*
:
CREATE TABLE `category` (
`categoryID` smallint(5) unsigned NOT NULL auto_increment,
`categoryParentID` smallint(5) unsigned NOT NULL default '0',
`categoryName` varchar(50) NOT NULL default '',
PRIMARY KEY (`categoryID`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
INSERT INTO `category` ( `categoryParentID`, `categoryName`) VALUES
(0, ' '),
(1, ' '),
(1, ' '),
(1, ' '),
(2, ' '),
(2, '333332'),
(2, '234234'),
(3, 'aqqqqqd'),
(4, ' '),
(5, '66333666');
*/
// id $category_id,
//$default_category
function Get_Category($category_id = 0,$level = 0, $default_category = 0)
{
global $DB;
$sql = "SELECT * FROM category ORDER BY categoryID DESC";
$result = $DB->query( $sql );
while ($rows = $DB->fetch_array($result))
{
$category_array[$rows[categoryParentID]][$rows[categoryID]] = array('id' => $rows[categoryID], 'parent' => $rows[categoryParentID], 'name' => $rows
[categoryName]);
}
if (!isset($category_array[$category_id]))
{
return "";
}
foreach($category_array[$category_id] AS $key => $category)
{
if ($category['id'] == $default_category)
{
echo "<option selected value=".$category['id']."";
}else
{
echo "<option value=".$category['id']."";
}
if ($level > 0)
{
echo ">" . str_repeat( " ", $level ) . " " . $category['name'] . "</option>/n";
}
else
{
echo ">" . $category['name'] . "</option>/n";
}
Get_Category($key, $level + 1, $default_category);
}
unset($category_array[$category_id]);
}
/*
:
Array
(
[1] => Array ( [id] => 1 [name] => [level] => 0 [ParentID] => 0 )
[4] => Array ( [id] => 4 [name] => [level] => 1 [ParentID] => 1 )
[9] => Array ( [id] => 9 [name] => [level] => 2 [ParentID] => 4 )
[3] => Array ( [id] => 3 [name] => [level] => 1 [ParentID] => 1 )
[8] => Array ( [id] => 8 [name] => aqqqqqd [level] => 2 [ParentID] => 3 )
[2] => Array ( [id] => 2 [name] => [level] => 1 [ParentID] => 1 )
[7] => Array ( [id] => 7 [name] => 234234 [level] => 2 [ParentID] => 2 )
[6] => Array ( [id] => 6 [name] => 333332 [level] => 2 [ParentID] => 2 )
[5] => Array ( [id] => 5 [name] => [level] => 2 [ParentID] => 2 )
[10] => Array ( [id] => 10 [name] => 66333666 [level] => 3 [ParentID] => 5 )
)
*/
// id,
function Category_array($category_id = 0,$level=0)
{
global $DB;
$sql = "SELECT * FROM category ORDER BY categoryID DESC";
$result = $DB->query($sql);
while ($rows = $DB->fetch_array($result))
{
$category_array[$rows['categoryParentID']][$rows['categoryID']] = $rows;
}
foreach ($category_array AS $key=>$val)
{
if ($key == $category_id)
{
foreach ($val AS $k=> $v)
{
$options[$k] =
array(
'id' => $v['categoryID'], 'name' => $v['categoryName'], 'level' => $level, 'ParentID'=>$v['categoryParentID']
);
$children = Category_array($k, $level+1);
if (count($children) > 0)
{
$options = $options + $children;
}
}
}
}
unset($category_array[$category_id]);
return $options;
}
?>
<?php
class cate
{
function Get_Category($category_id = 0,$level = 0, $default_category = 0)
{
echo $category_id;
$arr = array(
'0' => array(
'1' => array('id' => 1, 'parent' => 0, 'name' => '1111'),
'2' => array('id' => 2, 'parent' => 0, 'name' => '2222'),
'4' => array('id' => 4, 'parent' => 0, 'name' => '4444')
),
'1' => array(
'3' => array('id' => 3, 'parent' => 1, 'name' => '333333'),
'5' => array('id' => 5, 'parent' => 1, 'name' => '555555')
),
'3' => array(
'6' => array('id' => 6, 'parent' => 3, 'name' => '66666'),
'7' => array('id' => 7, 'parent' => 3, 'name' => '77777')
),
'4' => array(
'8' => array('id' => 8, 'parent' => 4, 'name' => '8888'),
'9' => array('id' => 9, 'parent' => 4, 'name' => '9999')
)
);
if (!isset($arr[$category_id]))
{
return "";
}
foreach($arr[$category_id] AS $key => $cate)
{
if ($cate['id'] == $default_category)
{
$txt = "<option selected value=".$cate['id']."";
}else{
$txt = "<option value=".$cate['id']."";
}
if ($level > 0)
{
$txt1 = ">" . str_repeat( "-", $level ) . " " . $cate['name'] . "</option>/n";
}else{
$txt1 = ">" . $cate['name'] . "</option>/n";
}
$val = $txt.$txt1;
echo $val;
self::Get_Category($key, $level + 1, $default_category);
}
}
function getFlush($category_id = 0,$level = 0, $default_category = 0)
{
ob_start();
self::Get_Category($category_id ,$level, $default_category);
$out = ob_get_contents();
ob_end_clean();
return $out;
}
}
$id =$_GET['id'];
echo "<select>";
$c = new cate();
//$c->Get_Category();
$ttt= $c->getFlush($id,'0','3');
echo $ttt;
echo "</select>";
?>