当前位置:网站首页>Common PHP interview questions (1) (written PHP interview questions)
Common PHP interview questions (1) (written PHP interview questions)
2022-07-05 15:05:00 【Back end regular developers】
List of articles
- 1. The way PHP Value transfer and reference of
- 2. Object oriented concept and understanding
- 3. Talk about dependency injection
- 4. Differences and uses between static classes and static methods
- 5. Write the following output
- 6. Please use PHP Write a function to verify whether the email format is correct ( Require regular )
- 7. <? echo “Hello CSDN”; ?> No output , What might be the reason . Briefly describe the process of solving this problem
- 8. In a function ( This function doesn't have return sentence ) Inside to deal with global variables , And change his value , There are two ways to achieve (global And quotes &)
- 9. Write a function , Can traverse all the files and subfolders under a folder .( Directory operation )
- 10. Can make HTML and PHP Separate the templates used
- 11.PHP The method of realizing infinite classification
- 12. List several things you know PHP The method of traversing or iterating the array
- 13. Enter the web address in the browser to the page display , What happened during ?
1. The way PHP Value transfer and reference of
php Pass value : Within the scope of the function , Change the value of the variable , It will not affect the variable value outside the function .
PHP By reference : Within the scope of the function , Any change to the value , It's also reflected outside the function , Because it's a memory address .
Advantages and disadvantages : It takes a lot of time to transfer values , Especially for large strings and objects , It will be a costly operation , Transfer reference , Any operation within a function is equivalent to an operation on a pass variable , High efficiency in transmitting large variables !
2. Object oriented concept and understanding
( encapsulation 、 Inherit 、 polymorphic )
3. Talk about dependency injection
PHP Principle and usage of dependency injection
4. Differences and uses between static classes and static methods
- Static class : The so-called static class means that it does not need to be instantiated into an object , Classes called directly in a static way , Suitable for making some stateless tools
- Static methods :
class Test{
const AI = "AI";
public static $a = "a";
public static function aAction(){
//TODO
}
}
Test::aAction();
Methods like this that can be called directly outside without instantiating classes are called static methods .
5. Write the following output
//(1)
$b = 100;
$c = 50;
$a = $b>$c?100:50;
echo $a; //100
//(2)
$str = “php”;
$$str = “teems”;
$$str .= “coffice”;
echo $php; //teemscoffice
//(3)
$a = “b”;
$b = “a”;
$ar[$a] = $b;
$ar[$b] = $a;
echo $ar[‘a’]; //b
//(4)
$n = 3.5;
$a = floor($n);
$b = round($n);
echo $a.$b; //34
echo $a+$b; //7
// notes :floor(): Rounding down
//round() 4 House 5 Rounding in
//$a=3;$b=4;
6. Please use PHP Write a function to verify whether the email format is correct ( Require regular )
$gz="/^[0-9a-z]+([-_.][0-9a-z]+)*@([0-9a-z]+\.[a-z]{2,14}(\.[a-z]{2,3})?)$/i";
$email = "[email protected]";
if(preg_match($gz,$email)){
echo " The format is correct ";
}else{
echo " Wrong format ";
}
7. <? echo “Hello CSDN”; ?> No output , What might be the reason . Briefly describe the process of solving this problem
Maybe there is no short tag on the server short_open_tag = Set to Off,php.ini Open the short label control parameters : short_open_tag = On
8. In a function ( This function doesn't have return sentence ) Inside to deal with global variables , And change his value , There are two ways to achieve (global And quotes &)
// Use Global
$var = 1;
function get_pra(){
global $var;
$var = "xxx";
echo $var;
}
echo $var."--";
get_pra();
echo $var;
// Use &
$var1 = 1;
function get_pra1(&$val){
$val +=1;
}
echo $var1."--";
get_pra1($var1);
echo $var1;
9. Write a function , Can traverse all the files and subfolders under a folder .( Directory operation )
listDir($dir);
function listDir($dir="./"){
if (is_dir($dir)){
$handle = opendir($dir);
while (($file = readdir($handle)) !== false){
if ($file == "." || $file == ".."){
continue;
}
if (is_dir($sub_dir = realpath($dir.'/'.$file))){
echo " Files in the folder :".$dir.":".$file.PHP_EOL;
listDir($sub_dir);
}else{
echo " The file named :".$file.PHP_EOL;
}
}
closedir($handle);
}else{
echo $dir." Not a directory ";
}
}
10. Can make HTML and PHP Separate the templates used
- Smarty
- Template
- Blade(Laravel Of )
11.PHP The method of realizing infinite classification
Recursion and reference (&)
$array = array(
array('id' => 1, 'pid' => 0, 'name' => ' Hebei Province '),
array('id' => 2, 'pid' => 0, 'name' => ' The Beijing municipal '),
array('id' => 3, 'pid' => 1, 'name' => ' Handan City '),
array('id' => 4, 'pid' => 2, 'name' => ' Chaoyang District '),
array('id' => 5, 'pid' => 2, 'name' => ' Tongzhou District '),
array('id' => 6, 'pid' => 4, 'name' => ' Wangjing '),
array('id' => 7, 'pid' => 4, 'name' => ' Jiuxianqiao '),
array('id' => 8, 'pid' => 3, 'name' => ' Yongnian District '),
array('id' => 9, 'pid' => 1, 'name' => ' Wu'an city '),
);
/** * Recursive implementation of infinite classification * @param $array Classified data * @param $pid Father ID * @param $level Classification level * @return $list An array of classes Just go through it $level Can be used to traverse indents */
function getTree($array, $pid =0, $level = 0){
// Declare a static array , Avoid recursive calls , Multiple declarations cause the array to overwrite
static $list = [];
foreach ($array as $key => $value){
// First traversal , Find the node whose parent node is the root node That is to say pid=0 The node of
if ($value['pid'] == $pid){
// The node whose parent node is the root node , The level of 0, That is, the first level
$value['level'] = $level;
// Put the array in list in
$list[] = $value;
// Remove this node from the array , Reduce the consumption of subsequent recursion
unset($array[$key]);
// Start recursion , Find parent ID For this node ID The node of , The level is the original level +1
getTree($array, $value['id'], $level+1);
}
}
return $list;
}
/* * Get the recursive data , Traversal generates classification */
$array = getTree($array);
foreach($array as $value){
echo str_repeat('--', $value['level']), $value['name'].PHP_EOL;
}
/** * Recursive reference infinite classification * @param $array Classified data * @return array An array of classes Just go through it */
function generateTree($array){
// First step Structural data
$items = array();
foreach($array as $value){
$items[$value['id']] = $value;
}
// Second parts Traversal data Generate tree structure
$tree = array();
foreach($items as $key => $value){
if(isset($items[$value['pid']])){
$items[$value['pid']]['son'][] = &$items[$key];
}else{
$tree[] = &$items[$key];
}
}
return $tree;
}
$array = generateTree($array);
var_dump($array);
12. List several things you know PHP The method of traversing or iterating the array
- For loop
<?php
for ($i=1; $i<=5; $i++)
{
echo " The number is " . $i . PHP_EOL;
}
?>
- Foreach
foreach ($array as $key => $value)
{
To execute code ;
}
- each
- list
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
reset($people);
while (list($key, $val) = each($people))
{
echo "$key => $val<br>";
}
?>
13. Enter the web address in the browser to the page display , What happened during ?
Generally speaking, it is divided into the following processes :
- DNS analysis : Resolve the domain name to IP Address
- TCP Connect :TCP Three handshakes
- send out HTTP request
- The server processes the request and returns HTTP message
- Browser parse render page
- disconnect :TCP Four waves
边栏推荐
猜你喜欢
【NVMe2.0b 14-9】NVMe SR-IOV
Interpretation of Apache linkage parameters in computing middleware
Huiyuan, 30, is going to have a new owner
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
Mongdb learning notes
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Machine learning notes - gray wolf optimization
Crud de MySQL
数据库学习——数据库安全性
随机推荐
我想咨询一下,mysql一个事务对于多张表的更新,怎么保证数据一致性的?
CODING DevSecOps 助力金融企业跑出数字加速度
Jmeter性能测试:ServerAgent资源监控
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Machine learning notes - gray wolf optimization
Isn't it right to put money into the external market? How can we ensure safety?
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
Your childhood happiness was contracted by it
计算中间件 Apache Linkis参数解读
漫画:优秀的程序员具备哪些属性?
Fr exercise topic --- comprehensive question
Brief introduction of machine learning framework
CPU设计实战-第四章实践任务三用前递技术解决相关引发的冲突
Interpretation of Apache linkage parameters in computing middleware
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
Can I pass the PMP Exam in 20 days?
"Sequelae" of the withdrawal of community group purchase from the city
Crud de MySQL
I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?