Free Online Computer Lessons Learn from the Pros! Fri, 29 Oct 2010 05:22:16 +0000 en-US hourly 1 Lazy: Change the way you code and speed up php. Fri, 29 Oct 2010 05:19:24 +0000 Echo vs Print – With current versions of PHP Echo is the way to go. When you have to print multiple strings, Echo allows you to skip the overhead of concatenation when you use “,” instead of “.” between strings.

Single vs Double quote – when possible use single quote, as double quote strings are processed by PHP.

Accessing data in Arrays – When working with an associative array, PHP operates quickest when the variable array is enclosed in single quotes such as $row[‘thomas’].

Function vs no functions – Everything is passed by reference after PHP 5, which significantly speeds things up, however they are still less efficient than code without functions. If you reuse the code multiple times within the document, functions are a must. If you are only using it once, it is just extra overhead. (This statement is against everything taught in school.)

Extra variables – Try to use as few variables as possible. For example:
$tomorrow=$a + (24*60*60);
is less efficient than

Count () in for loop – There are exceptions to the ‘use as few variables as possible’ rule. The most common is when you have the count function in a conditional loop.

Unset – when you are done using a variable, you can regain the memory by using unset.

Close connections /file handers – Give PHP back the resources when they are not needed.

Cache page if possible.

Lazy way to test

$time = microtime(true);
# code to be benchmarked here
echo '

Time elapsed: ',microtime(true) - $time, ' seconds';

]]> 0
SEO Pro Tip: Check your domain redirects Fri, 29 Oct 2010 02:46:26 +0000 Want your links juice to flow from your old domain to your new one? Make sure you redirect it properly with a 301 redirect. Use the form below to test if your URL redirect is SEO friendly.

URL to Check:
]]> 0
Speed up MySQL Call from PHP using mysql_unbuffered_query Wed, 21 Apr 2010 16:41:08 +0000 MySQL_unbuffered_query can dramatically speed up your code

When you make a call to mysql_query, all of the results get buffered to the server before continuing through the code. If you have a large result set, mysql_unbuffered_query() can speed up your process. Here is what says:

mysql_unbuffered_query() sends the SQL query query to MySQL without automatically fetching and buffering the result rows as mysql_query() does. This saves a considerable amount of memory with SQL queries that produce large result sets, and you can start working on the result set immediately after the first row has been retrieved as you don’t have to wait until the complete SQL query has been performed. To use mysql_unbuffered_query() while multiple database connections are open, you must specify the optional parameter link_identifier to identify which connection you want to use.

You can use this in your code like this: Do not forget to specify the link_identifier if using multiple connections.
$result= mysql_unbuffered_query ("select SQL_CALC_ROWS id as number_of_rows, name from test");
One other note: Note: With mysql_unbuffered_query(), You cannot use mysql_num_rows() but you can use SQL_CALC_ROWS on MySQL to get the total rows without the limit.

]]> 0
Replace text with a MySQL SQL statement Mon, 12 Apr 2010 18:24:19 +0000 Many times you will need to replace or reformat some data in your MySQL table. The replace can be done quickly with the MySQL REPLACE() command that looks like:
UPDATE `table_name` SET `field_name` = REPLACE(`field_name`, 'text to replace', 'new text');
Always make sure to update your tables before running any updates.

]]> 0
Set PHP Max Execution Time Fri, 09 Apr 2010 17:43:09 +0000 The default max execution time defined in php.ini is 30 seconds. It is possible that you want to alter this max execution time at run time. To do this, insert the below code in your php file.
set_time_limit ( int $seconds )
The number of seconds specified will be in addition to the time already used to execute the php script (the execution timer gets reset to zero when set_time_limit is called).

]]> 0
Quick PHP MySQL Connection Demo Fri, 09 Apr 2010 17:35:08 +0000 Quick reference on how to connect to a MySQL database from PHP. There are other possibilities but this is a quick demo.


mysql_connect($db_host,$db_user, $db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$result=mysql_query("select test from test_table");
    echo $row['test'] , '
'; }
]]> 0
Display PHP Warning and Error Messages Sat, 27 Dec 2008 20:37:36 +0000 Many webhost have php set to suppress error message in the php.ini.  Often times, a shared hosting user will not have access to modify this file.  If you still need to see the php errors, warnings, and notices, php has provided another solution:

ini_set("display_errors", 1);

Make sure to set this at the top of your document before any code has been outputted by the server.

]]> 1