Free Online Computer Lessons http://www.onlinecomputerlessons.com Learn from the Pros! Fri, 29 Oct 2010 05:22:16 +0000 en-US hourly 1 http://wordpress.org/?v=3.8.2 Lazy: Change the way you code and speed up php. http://www.onlinecomputerlessons.com/lazy-change-the-way-you-code-and-speed-up-php/ http://www.onlinecomputerlessons.com/lazy-change-the-way-you-code-and-speed-up-php/#comments Fri, 29 Oct 2010 05:19:24 +0000 http://www.onlinecomputerlessons.com/?p=54 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:
$a=time();
$tomorrow=$a + (24*60*60);
is less efficient than
$tomorrow=$time+(24*60*60);

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';

]]>
http://www.onlinecomputerlessons.com/lazy-change-the-way-you-code-and-speed-up-php/feed/ 0
SEO Pro Tip: Check your domain redirects http://www.onlinecomputerlessons.com/seo-301-redirect-checker/ http://www.onlinecomputerlessons.com/seo-301-redirect-checker/#comments Fri, 29 Oct 2010 02:46:26 +0000 http://www.onlinecomputerlessons.com/?p=32 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:
]]>
http://www.onlinecomputerlessons.com/seo-301-redirect-checker/feed/ 0
Speed up MySQL Call from PHP using mysql_unbuffered_query http://www.onlinecomputerlessons.com/speed-up-mysql-call-from-php-using-mysql_unbuffered_query/ http://www.onlinecomputerlessons.com/speed-up-mysql-call-from-php-using-mysql_unbuffered_query/#comments Wed, 21 Apr 2010 16:41:08 +0000 http://onlinecomputerlessons.com/?p=20 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 php.net 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.

]]>
http://www.onlinecomputerlessons.com/speed-up-mysql-call-from-php-using-mysql_unbuffered_query/feed/ 0
Replace text with a MySQL SQL statement http://www.onlinecomputerlessons.com/replace-text-with-a-mysql-sql-statement/ http://www.onlinecomputerlessons.com/replace-text-with-a-mysql-sql-statement/#comments Mon, 12 Apr 2010 18:24:19 +0000 http://onlinecomputerlessons.com/?p=17 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.

]]>
http://www.onlinecomputerlessons.com/replace-text-with-a-mysql-sql-statement/feed/ 0
Set PHP Max Execution Time http://www.onlinecomputerlessons.com/set-php-max-execution-time/ http://www.onlinecomputerlessons.com/set-php-max-execution-time/#comments Fri, 09 Apr 2010 17:43:09 +0000 http://onlinecomputerlessons.com/?p=10 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).

]]>
http://www.onlinecomputerlessons.com/set-php-max-execution-time/feed/ 0
Quick PHP MySQL Connection Demo http://www.onlinecomputerlessons.com/quick-php-mysql-connection-demo/ http://www.onlinecomputerlessons.com/quick-php-mysql-connection-demo/#comments Fri, 09 Apr 2010 17:35:08 +0000 http://onlinecomputerlessons.com/?p=8 Quick reference on how to connect to a MySQL database from PHP. There are other possibilities but this is a quick demo.

$db_host="localhost";
$db_user="test";
$db_password="test";
$db_name="test";

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");
while($row=mysql_fetch_array($result))
{
    echo $row['test'] , '
'; }
]]>
http://www.onlinecomputerlessons.com/quick-php-mysql-connection-demo/feed/ 0
Display PHP Warning and Error Messages http://www.onlinecomputerlessons.com/hello-world/ http://www.onlinecomputerlessons.com/hello-world/#comments Sat, 27 Dec 2008 20:37:36 +0000 http://onlinecomputerlessons.com/?p=1 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:

error_reporting(E_ALL);
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.

]]>
http://www.onlinecomputerlessons.com/hello-world/feed/ 1