• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

40+ Useful Php tips for beginners ? Part 1

php 搞代码 4年前 (2022-01-23) 29次浏览 已收录 0个评论

PHP学习指南

http://www.binarytides.com/category/php-2/tutorial/

In this series we are going to look into some useful tips and techniques that can be used to improve and optimise your php code. Note that these php tips are meant for beginners and not those who are already using mvc frameworks etc.

The Techniques 1. Do not use relative paths , instead define a ROOT path

Its quite common to see such lines :

require_once('http://www.cnblogs.com/lib/some_class.php');

This approach has many drawbacks :

It first searches for directories specified in the include paths of php , then looks from the current directory.
So many directories are checked.

When a script is included by another script in a different directory , its base directory changes to that of the including script.

Another issue , is that when a script is being run from cron , it may not have its parent directory as the working directory.

So its a good idea to have absolute paths :

define('ROOT' , '/var/www/project/');require_once(ROOT . 'http://www.cnblogs.com/lib/some_class.php');//rest of the code

Now this is an absolute path and will always stay constant. But we can improve this further. The directory /var/www/project can change , so do we change it everytime ? No instead we make it portable using magic constants like __FILE__ . Take a closer look :

//suppose your script is /var/www/project/index.php//Then __FILE__ will always have that full path.define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME));require_once(ROOT . 'http://www.cnblogs.com/lib/some_class.php');//rest of the code

So now even if you shift your project to a different directory , like moving it to an online server , the same code will run without any changes.

2. Dont use require , include , require_once or include_once

Your script could be including various files on top , like class libraries , files for utility and helper functions etc like this :

require_once('lib/Database.php');require_once('lib/Mail.php');require_once('helpers/utitlity_functions.php');

This is rather primitive. The code needs to be more flexible. Write up helper functions to include things more easily. Lets take an example :

function load_class($class_name){    //path to the class file    $path = ROOT . '/lib/' . $class_name . '.php');    require_once( $path ); }load_class('Database');load_class('Mail');

See any difference ? You must. It does not need any more explanation.
You can improve this further if you wish to like this :

function load_class($class_nam<a>@本文9来源gao($daima.com搞@代@#码8网^</a><strong>搞代gaodaima码</strong>e){    //path to the class file    $path = ROOT . '/lib/' . $class_name . '.php');        if(file_exists($path))    {        require_once( $path );     }}

There are a lot of things that can be done with this :

Search multiple directories for the same class file.
Change the directory containing class files easily , without breaking the code anywhere.
Use similar functions for loading files that contain helper functions , html content etc.

3. Maintain debugging environment in your application

During development we echo database queries , dump variables which are creating problems , and then once the problem is solved , we comment them or erase them. But its a good idea to let everything stay and help in the long run

On your development machine you can do this :

define('ENVIRONMENT' , 'development');if(! $db->query( $query ){    if(ENVIRONMENT == 'development')    {        echo "$query failed";    }    else    {        echo "Database error. Please contact administrator";    }    }

And on the server you can do this :

define('ENVIRONMENT' , 'production');if(! $db->query( $query ){    if(ENVIRONMENT == 'development')    {        echo "$query failed";    }    else    {        echo "Database error. Please contact administrator";    }    }

4. Propagate status messages via session


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:40+ Useful Php tips for beginners ? Part 1

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址