私人调试函数求优化
1.可能有点异想天开,我希望获取调用这个函数的文件的文件名和行数,目前的死办法是外面传__FILE__和__LINE__
2.我想获取外面传的调试变量的名称,例如你有个变量叫 $abc = 'fdsafs'; 这样赋值了,然后 debug($abc); 我能在这个函数内部想办法知道这个变量的名称叫 abc
3.获取变量类型,类似别的语言的typeof,现在来一个变量,假设它是字符串,却没办法echo typeof($data) ,却要if(is_string($data))echo 'string';...
4.你能提出的更多优化建议,写一写你的改造结果~
====================
这个函数从当初最简单的
function debug($data, $die = false){
echo '
';<br> print_r($data);<br> echo '';
if($die)die();
}
到今天如下面的代码,已经是一年多了~方便自己调试变量值.我也懒得学那些什么复杂的调试框架什么xDebug..其实我要调试的需求在上面说的和下面代码体现的都有了,没更多的要求.
大家有没有试过,在一个很大的程序体里,到处echo一些值,在调试完后修正代码后,运行时还发现有调试的echo值输出,却不记得这个echo放哪里了- - 所以我需要知道这个调用的文件位置和行号.包括其它更多的调试需求,其实也不是很多,知道变量内容,长度,类型,它变成这个内容的文件位置就行了.
上代码:
$a = 'xx';
$b = 45;
$c = true;
$d = false;
$e = null;
$f = new stdClass();
$g = array(
'a' => 'v1',
'b' => 'v2',
);
debug($a);
debug($b);
debug($c);
debug($d);
debug($d);
debug($e);
debug($f);
debug($g);
function debug($data, $die = false, $file = null, $line = null){
static $debugCount = 0; //调试计数器
echo '
================= 新的调试点: '.++$debugCount.'========================
';
if($file)echo "文件名: $file
\n";
if($line)echo "行数: $line
\n";
$len = strlen($data);
if($len)echo '内容长度:'.$len."
\n";
echo "变量名称: a
\n"; //有没有办法获取到外面传参时,debug($a);的变量名称 'a'?
echo "
\n";<br> print_r(var_export($data, true));<br> echo "\n";
if($die){
exit('
返回');
}
else return $debugCount;
}
------解决方案--------------------
function debug()<br />{<br /> global $config;<br /> if(!$config['debug']) {<br /> return;<br /> }<br /><br /> echo '<pre class="brush:php;toolbar:false">';<br /> foreach(func_get_args() as $var)<br /> var_dump($var);<br /> echo '';
}
function fdebug()
{
global $config;
if(!$config['debug']) {
return;
}
foreach(func_get_args() as $var) {
file_put_contents('./aaa_yii_debug.php', ''.PHP_EOL . PHP_EOL, FILE_APPEND);

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version