Monday, April 4, 2011

Rumors of iphone-5

There have been so many rumors about the iPhone 5. Lets take a look.
iPhone-5 rumor

For more details check out iPhone 5 Rumor Roundup @ mashable.

Saturday, March 26, 2011

Tips for optimizing Php code

This tutorial targets the Php developer who already understands Php, but wants to write more efficient Php code or to improve the performance of existing Php applications

Don't copy variables for no reason:
Sometimes PHP novices attempt to make their code "cleaner" by copying predefined variables to variables with shorter names before working with them. What this actually results in is doubled memory consumption (when the variable is altered), and therefore, slow scripts. In the following example, if a user had inserted 512KB worth of characters into a textarea field. This implementation would result in nearly 1MB of memory being used.

$description = strip_tags($_POST['description']);
echo $description;
You can simply do this operation inline and avoid the extra memory consumption:

echo strip_tags($_POST['description']);

Avoid doing SQL queries within a loop:
A common mistake is placing a SQL query inside of a loop. This results in multiple round trips to the database, and significantly slower scripts. In the example below, you can change the loop to build a single SQL query and insert all of your users at once.

foreach ($userList as $user) {
  $query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
  mysql_query($query);
  }

Instead use following piece of code:

$userData = array();
foreach ($userList as $user) {
    $userData[] = '("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
 }
$query = 'INSERT INTO users (first_name,last_name) VALUES' . implode(',', $userData);
mysql_query($query);

Query structure would look like this:

INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...


Eliminate redundant function calls:
Do not use functions inside of for loop, such as:
for ( $i=0; $i<count($myArray); ++$i )
 // do something

Although it appears innocent enough, that call to count($myArray) is made once every iteration. In any such loop, provided it doesn't add or remove elements from $myArray, the size of the array and hence the value returned by count($myArray) remains constant. You can write it more efficiently as follows:

$arraySize = count($myArray);
for ( $i=0; $i<$arraySize; ++$i )
// do something
Similarly
$myConstString = "This is some constant text";
for ( $i=0; $i<$arraySize; ++$i )
{
   echo trim(strtolower($myConstString));
   // do something
}
Provided the above loop does not modify $myConstString, then the call to trim(strtolower($myConstString)) will always return the same value (in this instance, a string). You can move this function call to just before the loop, and store the return value in a temporary variable.

Use isset() instead of strlen():
When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. This function is pretty quick since it's operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick.

if (strlen($foo) < 5)
  echo "Foo is too short";
Instead use:
if (!isset($foo{5}))
  echo "Foo is too short";
Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length.



Enjoy this post?
show your interest using comments.

Tuesday, July 27, 2010

Why use php ?

Why to use php instead of other web technology?
  • Php is open source & free software. Open source means the source code is freely available to study, to use or modify.
  • Also it is cross platform to develop, to deploy and to use i.e. it works on Widows server, on Mac server or on Linux server.
  • It’s powerful, robust & scalable. The important point which I want to highlight is that instead of being open source does not mean that it’s not powerful and scalable.
  • Version 5 of php is object oriented.
  • Php has great documentation in various languages.  Click here to visit
  • It has large, active developer community. They already create great example of Php application for example Wordpress, joomla  etc. are open source and help you to create websites like blog  and many more
By learning php you will be able to work with these tools more efficiently and even customized them according to your specific needs.


Enjoy this post?
show your interest using comments.

Monday, July 26, 2010

PHP : Introduction


Web Development is a process of developing website. It includes web designing , client/server side scripting , web server & security.

PHP is a server side scripting language and is used to create dynamic and interactive websites.
The term server side side scripting language means that the whole processing is done at server and finally the output is rendered at client whereas client side scripting language like javascript runs at client side.

When browser returns web page,what actually happening at back-end is, it is actually in HTML format because browser only understand HTML.

Lets look at example:

<html>
<head>
<title>Demo Page</title>
</head>
<body>

/* Server side script */
<?php
echo "Hello world";
?>
/* end script */

</body>
</html>


Now viewing page source ,

browser return html

If you see bugs, bad english, got questions or something tell me by using comments,

See you next time guys!


Enjoy this post?


show your interest using comments.