Archive for October, 2008

php code optimization tips

Do you know the ways to optimize your php code. Here we have some tips for you.

  • If a method can be static, declare it as static. It will improve the Speed by a factor of 4.
  • You should use echo as it is faster than print.
  • If you have to concat string, Use echo’s multiple parameters.
  • Set the value of how many times it should be iterated before the loop. Do not assign the value in the loop.
  • When a variable is no longer useful, unset those variables which will free memory.
  • Use absolute urls in includes and requires.
  • To get the time of when the script started its execution, you must prefer $_SERVER [‘REQUEST_TIME’] than time ()
  • Str_replace is faster than preg_replace
  • It is much better to utilize switch statements than multi if, else if statements.
  • Every time close your database connections when you got all the datas from DB
  • Always use $row [‘name’] to get data from DB.Because it is 7 times faster than $row [name]
  • Don’t use functions inside of for loop, such as for ($y=1; $y <= main ($arr); $y) the main () function gets called each time.
  • If you declare a global variable and you are not using it in any function will slow down the performance because will check if the global exists or not.
  • Methods should be in derived classes. This will run faster than the method which was defined in the base class.
  • Surrounding your string by single quote (‘) instead of double quotes (“) will be little faster as php looks for variables inside “” but not inside ”.
  • A PHP script will be at least 2-10 times slower than a static HTML page by Apache. So it is better to use lot of static HTML pages with less scripts.
  • You must install a PHP caching product to naturally boost the performance by 25-100% by eliminating compile times. Your PHP scripts will be recompiled each time if the scripts are not cached.
  • Do not use OOP concepts too much because each method and object call consumes a lot of memory.
  • Split methods that has the code you will really re-use too much.Do not split all the functions.

Tags:

Wednesday, October 29th, 2008 Tips No Comments

Are you a real programmer? How can you tell that?

This is really amusing for one and all to read.

Machine code is your pseudo code

You trust that by means of a linker or a macro library to make a program is “dirty”, a real programmer does not get baffled when the source code get to 6000 lines in one file

You suppose that PC playoffs are a squander of CPU cycles.

While you are at a monetary firm coding for mainframes in mainframe assembly (the manager inform him to code in Assembly rather than machine code so the other ‘normal’ programmers may possibly be aware of what is going on)

You construct your individual programming languages and provide them crappy names.

You were not being fond of multicore systems. You accept as true that if you cannot formulate a program to run fast enough on a single core, you are not a programmer.

You will never utilize operating system interrupts … you make your own.

If your random number generator creates the random numbers from zero to infinity in 5 seconds, you believe it as very slow.

You consider that remarking the source code is for the amateur.

You don’t make use of computers outside of work. You will not have belief on other programmer’s code.

You judge that technologies like PHP, Python or VBA were not a programming language- it is only scripting language.

You don’t want to use Object Orientation for the reason that it makes your work trouble-free. In any case you wish for a challenge.

C++ and APL programming languages was a toy to you.

Someone point out the languages like VB and JAVA to you, you will give a punch them on the head.

Even if your code has been typed up, it was already optimized.

You get clear of the Back Space key on the Keyboard as you do not make any mistakes.

You don’t like to have any book or notepads; it’s all in your head.

Your programs are not user friendly, even if they are bug free.

You have a nightmare of creating your own CPU for yourself one day and maintain it in your parent’s garage.

You thought that IDE’s, Programmer Tools and Syntax Highlighters are for people who do not be familiar with what they are doing.

You return back the computer mouse to the technician, with a screw driver stabbed through it. Real programmers will make use of command prompt only.

If the client does not be fond of what you perform for them, you think that the client is an idiot and you say “No” to alter anything.

Who wants friends when you can program your own friends.

Tags: , ,

Tuesday, October 21st, 2008 Programmer No Comments

How to handle errors using Boolean flags

This error handling method may be the most ancient. Boolean flags were used by many programming languages, and PHP will not be an exception. Even though they are very simple to apply, the main negative aspect is that they were not very informative about the mistake that happened and its perspective. Here is an unsophisticated implementation of the FileReader class, which make use of Boolean flags:

[php]
class FileReader{
var $file;
var $fileDir=’fileDir/';
function FileReader($file){
if(!@file_exists("{$this->fileDir}{$file}.php")){
return false;
}
$this->file=$file;
}
function getContent(){
if(!@$content=file_get_contents("{$this->fileDir}{$this->file}.php")){
return false;
}
return $content;
}
}
[/php]

Bearing in mind about the explanation for the above example, class errors might be handled as follows:

[php]
$fr=new FileReader(‘inexistent_file’);
if(!$fr->getContent()){
die(‘Unable to read file contents’);
}
else{
echo $fr->getContent();
}
[/php]

In the example, @ error suppression operator is intentionally used, so as to stay away from the complaints of the PHP interpreter and return a false value (or 0 or -1) when a failure happens. At first look, you can observe the incompetence of this method, plus its restricted flexibility. On the other hand, this approach has established to be quite victorious in procedural applications, or when client code is able to handle straightforward errors without corrupting the entire application.

Nowadays, we have investigated the pros and cons of common error handling approaches in PHP 4. Certainly, in huge web applications, a set of error controlling classes is preferred, so you are able to handle errors through a centralized point. Conversely, the trigger_error () / set_error_handler () combination may suit the requirements of small projects, so it is worth considering.

Tags: ,

Thursday, October 16th, 2008 Programmer, Programming, Troubleshoot No Comments

Guidelines to handle Db connection

Please confirm that you close the original connection to a database all the time and that you are using connection pooling to get better processing speed of database connection creation etc. Here are some tips for you:

1. Use web.config to store up the connection string:

2. Use this connection string forever and do not make any changes to it. If you were not doing this (for example using different DSNs) you will disable the connection pooling feature:
string dsn = System.Configuration.ConfigurationSettings.AppSettings[“dsn”];

3. Do not perform any tricks to try to deal with connections etc, just proceed and use it (no wrapper classes or so involved):
SqlConnection conn = new SqlConnection(dsn);

4. Formulate the time between conn.Open() and conn.Close() as small as possible. E.g. don’t do this:
SqlConnection conn = new SqlConnection(dsn);
Conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(…);

//and further cmd parameters actions and so on

SqlDataReader reader = cmd.ExecuteReader();

//use the reader and do a bundle of additional tasks, not relying on the database

conn.Close();

But rather, use:
SqlConnection conn = new SqlConnection(dsn);
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(…);

//and more cmd parameters actions etc

conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

//use the reader
conn.Close();
//perform a batch of other work, not relying on the database

5. Always close the connection when you are done, even when an exception occurs. To do this, use the try…finally pattern:
SqlConnection conn = new SqlConnection(dsn);
try
{
//some init depending on conn
conn.Open();
//minimum number of lines of code depending on the open connection
}
finally
{
if (conn.ConnectionState == ConnectionState.Open)
conn.Close();
}
or the using pattern in C#:
using (SqlConnection conn = new SqlConnection(dsn))
{
//…
conn.Open();
}
This will close the db connection repeatedly while leave from the using block.

Tags:

Friday, October 10th, 2008 Programmer No Comments

Copy to Clipboard with Javascript on Mozilla or Netscape Browsers

Hope all may know that by using Javascript select() and execCommand(“Copy”)
function we may able to select all the contents and copy those selected contents into our clipboard.

However, this function will work only in Internet Explorer (all version) by default, but not in Mozilla or Netscape browsers. To enable these Javascript Copy to Clipboard function in the Mozilla and Netscape Browsers, please follow the steps to provided below

1) Type about:config in the browser address bar.
2) Now, in the Filter window search for the following:
signed.applets.codebase_principal_support
3) Set the Boolean value to true for Firefox or enter true or 1 for Mozilla browsers.
Keep in mind that Preference names are case-sensitive.

Once you have made this simple settings on your Mozilla or netscape browsers you may use the copy to clipboard function on your browsers.

Tags:

Thursday, October 9th, 2008 Programming No Comments

Cannot able to close Form?

It is a fascinating bug that forces a Windows Form to go into a condition where it cannot be closed. Are you using the initial version of the .NET framework with SP1 installed?
Want to check this?

  • Build a new form, F1.
  • Set a user control U1 on top of it.
  • Locate one more user control, U2, onto the control U1.
  • To end with, put a button onto U2.
  • Write down an event handle for the button click that causes the user control U2 to be detached from its parent, U1.
  • Run the application.
  • Click on the button.
  • You should wind up with a form.
  • Now make an attempt to close the form by clicking on the close button.
  • It does not work. Am I right?
    Reason:
    The form is saying NO to close because its Close method is trying to authenticate the control first. The validation should fail because its unvalidatedControl private member still points to the button which has been detached from the hierarchy.

    How to overcome:
    After get rid of the user control U2, call a.OnControlRemoved (c) that clear the unvalidatedControl variable.

Tags:

Tuesday, October 7th, 2008 Programming No Comments