May 23, 2009 9

PHP programmer’s evolution scribble

By admin in php

The various stages you go through in your development career.

$mysql = mysql_connect('localhost', 'reinhold', 'secret_hash');
mysql_select_db('wordpress') or die("cannot select DB");

Trying a DRY approach

$db_host = 'localhost';
$db_user = 'reinhold';
$db_password = 'secret_hash';
$db_database = 'wordpress';
 
$mysql = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_database);

As the values normally don’t change, how about using constants

define('DB_HOST', 'localhost');
define('DB_USER', 'reinhold');
define('DB_PASSWORD', 'secret_hash');
define('DB_DATABASE', 'wordpress');
 
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABASE);

After years of changing the values every time you uplaod something to the live server

define('LIVE_ENV', true);
 
if(LIVE_ENV) {
	define('DB_HOST', 'localhost');
	define('DB_USER', 'reinhold');
	define('DB_PASSWORD', 'secret_hash');
	define('DB_DATABASE', 'wordpress');
} else {
	define('DB_HOST', 'testserver.com');
	define('DB_USER', 'reinhold');
	define('DB_PASSWORD', 'secret_hash');
	define('DB_DATABASE', 'wordpress');	
}
 
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABASE);

Even better would be this

if ($_SERVER["HTTP_HOST"] == 'www.domain.com')  // remote live environment
{ ... }
else // localhost test environment
{ ... }

PHP5 procedural approach using the new mysqli extension

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
 
if (!$link) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
printf("Host information: %s\n", mysqli_get_host_info($link));
mysqli_close($link);

Tags: ,

9 Responses to “PHP programmer’s evolution scribble”

  1. [...] Achei interessante e estou compartilhando com vocês um artigo que traduzi do blog de um desenvolvedor web Alemão: Reinhold Weber. [...]

  2. [...] Achei interessante e estou compartilhando com vocês um artigo que traduzi do blog de um desenvolvedor web Alemão: Reinhold Weber. [...]

  3. [...] DRY stands for Don’t Repeat Yourself, and it’s a valuable programming concept, no matter what the language. DRY programming, as the name implies, is ensuring that you don’t write redundant code. Here’s an example from Reinhold Weber: [...]

  4. [...] built-in function for that 100 lines of code, try combining your statements, and so on – here is a very good tutorial on this topic. Once you think that your code is perfect, it’s time to [...]

  5. [...] This post was Twitted by JonGauthier [...]

  6. [...] This post was Twitted by zamshed [...]

  7. [...] 40 Tips for optimizing your php code [...]

  8. [...] This seems like an obvious thing, but when working on multiple projects, over time you start to see ways you can improve your most used snippets of code. As a PHP developer I frequently use mysql_connect(), but have gotten to the point I dont even think about it as I have it included at the top of all my scripts. I just need to change the constants to define username, password, and database for the various projects I use it on. This can be further improved by adding conditional statements to differentiate between live and developement environments. This is an excellent example of evolving code. [...]

  9. [...] »优化PHP代码的40条建议原文作者:Reinhold Weber 原文链接:40 Tips for optimizing your php Code [...]

Leave a Reply

Comment Spam Protection by WP-SpamFree