Archive for the ‘PHP’ Category


>

Here is the good PDF file which explains how to install PostgreSQL in WAMP server.

Installing-WampServer2-0c-With-PostgreSQL

If you are using php 5.2.6, there is some problem in adapter of php5.2.6, thats it is unable connect to PostgreSQL. For making connection we are using adapter of php5.2.5. you can download dll files using below link if you dont find in online.

php5.2.4 dll files


Here is the good PDF file which explains how to install PostgreSQL in WAMP server.

Installing-WampServer2-0c-With-PostgreSQL

If you are using php 5.2.6, there is some problem in adapter of php5.2.6, thats it is unable connect to PostgreSQL. For making connection we are using adapter of php5.2.5. you can download dll files using below link if you dont find in online.

php5.2.4 dll files

>Find number of weeks in a Year

Posted: March 16, 2011 in PHP

>

$weeks_in_year = strftime(“%W”,strtotime(“12/31/”.date(‘Y’)));

>Get number of weeks between 2 dates

Posted: February 18, 2011 in PHP

>

function weeks_between($datefrom, $dateto)
{
    $datefrom_arr = explode(“-“, $datefrom);
    $datefrom = mktime(0,0,0,$datefrom_arr[1], $datefrom_arr[2], $datefrom_arr[0]);
                //echo date(‘Y-m-d’,mktime(0,0,0,$datefrom_arr[1], $datefrom_arr[2]+7, $datefrom_arr[0]));
               
                $dateto_arr = explode(“-“,$dateto);
                $dateto = mktime(0,0,0,$dateto_arr[1], $dateto_arr[2], $dateto_arr[0]);
               
    $day_of_week = date(“w”, $datefrom);
    $fromweek_start = $datefrom – ($day_of_week * 86400) – ($datefrom % 86400);
    $diff_days = days_between($datefrom, $dateto);
    $diff_weeks = intval($diff_days / 7);
    $seconds_left = ($diff_days % 7) * 86400;
    if( ($datefrom – $fromweek_start) + $seconds_left > 604800 )
        $diff_weeks ++;
    return $diff_weeks;
}
function days_between($datefrom,$dateto){
    $fromday_start = mktime(0,0,0,date(“m”,$datefrom),date(“d”,$datefrom),date(“Y”,$datefrom));
    $diff = $dateto – $datefrom;
    $days = intval( $diff / 86400 ); // 86400  / day
    if( ($datefrom – $fromday_start) + ($diff % 86400) > 86400 )
        $days++;
    return  $days;
}

>Send Text Messages with PHP

Posted: January 28, 2011 in PHP

>

PText messaging has become extremely widespread throughout the world — to the point where an increasing number of web applications have integrated SMS to notify users of events, sales or coupons directly through their mobile devices.

For more Information:

http://net.tutsplus.com/tutorials/php/how-to-send-text-messages-with-php/


>

We can now create desktop applications without learning a completely new programming language! That is with the help of a free and open source tool called “Titanium”. Whats more you will use your existing CSS and Javascript and PHP knowledge.

For more Information Visit:

http://www.sanisoft.com/blog/2011/01/03/introduction-to-creating-desktop-applications-with-php-and-titanium/


>function yourtheme_preprocess_node(&$variables) {
if ($your_condition) {
drupal_add_css(path_to_theme(). “/filename.css”, “theme”);
$variables[‘styles’] = drupal_get_css();

drupal_add_js(file_directory_path() .’/javascript.js’, ‘inline’);
$vars[‘scripts’] = drupal_get_js();
}
}

>Simpletest: submitFormByName

Posted: July 1, 2010 in PHP

>The SimpleTest PHP unit tester and web test framework.It has support for SSL, forms, frames, proxies and basic authentication. The idea is that common but fiddly PHP tasks, such as logging into a site, can be tested easily.

Unfortunately Simpletest doesnot provide a function for submit a form with its Name.But we can implement this function by 3 small changes in 3 different files.

1)browser.php 
        In this add below code

    function submitFormByName($name) {
        if (! ($form = &$this->_page->getFormByName($name))) {
            return false;
        }
        $success = $this->_load(
                $form->getAction(),
                $form->submit());
        return ($success ? $this->getContent() : $success);
    }

2)Page.php
        Add below function

    function &getFormByName($name) {
        for ($i = 0; $i < count($this->_complete_forms); $i++) {
            if ($this->_complete_forms[$i]->getName() == $name) {
                return $this->_complete_forms[$i];
            }
        }
        $null = null;
        return $null;
    }

3)Form.php

         Here u have to declare variable caleed
                     $this->_name = $tag->getAttribute(‘name’); 
        in constructor(SimpleForm) then add the function

    function getName() {
        return $this->_name;
    }

 Thats all. Now you can submit a form with its name also.


      

>Highlight Search Words

Posted: June 17, 2010 in PHP

>

String Manipulation

This first method will highlight all the occurances of the string and is case sensitive. It uses str_replace to do the required manipulation but has faults in not being able to tell the difference between PHP and PHPRO. It will highlight the text “PHP” and “PHP”RO which may be the desired result.


<?php

/** 
 * 
 * @highlight words 
 * 
 * @param string $string 
 * 
 * @param array $words 
 * 
 * @return string 
 * 
 */
 
function highlightWords($string$words)
 {
    foreach ( 
$words as $word )
    {
        
$string str_ireplace($word'<span class="highlight_word">'.$word.'</span>'$string);
    }
    
/*** return the highlighted string ***/
    
return $string;
 }

/*** example usage ***/
$string 'This text will highlight PHP and SQL and sql but not PHPRO or MySQL or sqlite';
/*** an array of words to highlight ***/
$words = array('php''sql');
/*** highlight the words ***/
$string =  highlightWords($string$words);

?>

<html>
<head> 
<title>PHPRO Highlight Search Words</title> 
<style type="text/css">
.highlight_word{
    background-color: pink;
}
</style> 
</head>
<body>
 <?php echo $string?>
</body>
</html>

Regular Expression

This second method makes us of PHP PCRE to achieve a better result. The seach is case insensitive, which means it will match php and PHP. This method has the added benifit of being able to use word boundries which enables highlighting of the word PHP but not PHPRO. The word boundary prevents partial matching of the search text and of highlighting parts of words. If this is the functionality you require, this is the method to choose.


 <?php

/**
 * @highlight words
 *
 * @param string $text
 *
 * @param array $words
 *
 * @return string
 *
 */
function highlightWords($text$words)
{
        
/*** loop of the array of words ***/
        
foreach ($words as $word)
        {
                
/*** quote the text for regex ***/
                
$word preg_quote($word);
                
/*** highlight the words ***/
                
$text preg_replace("/\b($word)\b/i"'<span class="highlight_word">\1</span>'$text);
        }
        
/*** return the text ***/
        
return $text;
}

/*** example usage ***/
$string 'This text will highlight PHP and SQL and sql but not PHPRO or MySQL or sqlite';
/*** an array of words to highlight ***/
$words = array('php''sql');
/*** highlight the words ***/
$string =  highlightWords($string$words);

?>

<html>
<head>
<title>PHPRO Highlight Search Words</title>
<style type="text/css">
.highlight_word{
        background-color: pink;
}
</style>
</head>
<body>
 <?php echo $string?>
</body>
</html>

>50+ PHP optimisation tips

Posted: June 17, 2010 in PHP

>

  1.  echo is faster than print
  2. Wrap your string in single quotes (‘) instead of double quotes (“) is faster because PHP searches for variables inside “…” and not in ‘…’, use this when you’re not using variables you need evaluating in your string. 
  3. Use sprintf instead of variables contained in double quotes, it’s about 10x faster. 
  4. Use echo’s multiple parameters (or stacked) instead of string concatenation. 
  5. Use pre-calculations, set the maximum value for your for-loops before and not in the loop. ie: for ($x=0; $x < count($array); $x), this calls the count() function each time, use $max=count($array) instead before the for-loop starts.
  6. Unset or null your variables to free memory, especially large arrays.
  7. Avoid magic like __get, __set, __autoload. 
  8. Use require() instead of require_once() where possible. 
  9. Use full paths in includes and requires, less time spent on resolving the OS paths. 
  10. require() and include() are identical in every way except require halts if the file is missing. Performance wise there is very little difference. 
  11. Since PHP5, the time of when the script started executing can be found in $_SERVER[’REQUEST_TIME’], use this instead of time() or microtime(). 
  12. PCRE regex is quicker than EREG, but always see if you can use quicker native functions such as strncasecmp, strpbrk and stripos instead. [Citation]
  13. When parsing with XML in PHP try xml2array, which makes use of the PHP XML functions, for HTML you can try PHP’s DOM document or DOM XML in PHP4. 
  14. str_replace is faster than preg_replace, str_replace is best overall, however strtr is sometimes quicker with larger strings. Using array() inside str_replace is usually quicker than multiple str_replace. 
  15. “else if” statements are faster than select statements aka case/switch.
  16. Error suppression with @ is very slow.
  17. To reduce bandwidth usage turn on mod_deflate in Apache v2  or for Apache v1 try mod_gzip. 
  18. Close your database connections when you’re done with them. 
  19. $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant.
  20. Use tags when declaring PHP as all other styles are depreciated, including short tags. 
  21. Use strict code, avoid suppressing errors, notices and warnings thus resulting in cleaner code and less overheads. Consider having error_reporting(E_ALL) always on.
  22. PHP scripts are be served at 2-10 times slower by Apache httpd than a static page. Try to use static pages instead of server side scripts.
  23. PHP scripts (unless cached) are compiled on the fly every time you call them. Install a PHP caching product (such as memcached or eAccelerator or Turck MMCache) to typically increase performance by 25-100% by removing compile times. You can even setup eAccelerator on cPanel using EasyApache3
  24. An alternative caching technique when you have pages that don’t change too frequently is to cache the HTML output of your PHP pages. Try Smarty or Cache Lite
  25. Use isset where possible in replace of strlen. (ie: if (strlen($foo) < 5) { echo “Foo is too short”; } vs. if (!isset($foo{5})) { echo “Foo is too short”; } ). 
  26. ++$i is faster than $ i++, so use pre-increment where possible.
  27. Make use of the countless predefined functions of PHP, don’t attempt to build your own as the native ones will be far quicker; if you have very time and resource consuming functions, consider writing them as C extensions or modules. 
  28. Profile your code. A profiler shows you, which parts of your code consumes how many time. The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks in overview. 
  29. Document your code. 
  30. Learn the difference between good and bad code. 
  31. Stick to coding standards, it will make it easier for you to understand other people’s code and other people will be able to understand yours. 
  32. Separate code, content and presentation: keep your PHP code separate from your HTML. 
  33. Don’t bother using complex template systems such as Smarty, use the one that’s included in PHP already, see ob_get_contents and extract, and simply pull the data from your database. 
  34. Never trust variables coming from user land (such as from $_POST) use mysql_real_escape_string when using mysql, and htmlspecialchars when outputting as HTML.
  35. For security reasons never have anything that could expose information about paths, extensions and configuration, such as display_errors or phpinfo() in your webroot.
  36. Turn off register_globals (it’s disabled by default for a reason!). No script at production level should need this enabled as it is a security risk. Fix any scripts that require it on, and fix any scripts that require it off using unregister_globals(). Do this now, as it’s set to be removed in PHP6. 
  37. Avoid using plain text when storing and evaluating passwords to avoid exposure, instead use a hash, such as an md5 hash.
  38. Use ip2long() and long2ip() to store IP addresses as integers instead of strings.
  39. You can avoid reinventing the wheel by using the PEAR project, giving you existing code of a high standard. 
  40. When using header(‘Location: ‘.$url); remember to follow it with a die(); as the script continues to run even though the location has changed or avoid using it all together where possible. 
  41. In OOP, if a method can be a static method, declare it static. Speed improvement is by a factor of 4. 
  42. Incrementing a local variable in an OOP method is the fastest. Nearly the same as calling a local variable in a function and incrementing a global variable is 2 times slow than a local variable. 
  43. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable. 
  44. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. 
  45. Just declaring a global variable without using it in a function slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists. 
  46. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance. 
  47. Methods in derived classes run faster than ones defined in the base class. 
  48. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.
  49. Not everything has to be OOP, often it is just overhead, each method and object call consumes a lot of memory.
  50. Never trust user data, escape your strings that you use in SQL queries using mysql_real_escape_string, instead of mysql_escape_string or addslashes. Also note that if magic_quotes_gpc is enabled you should use stripslashes first.
  51. Avoid the PHP mail() function header injection issue.
  52. Unset your database variables (the password at a minimum), you shouldn’t need it after you make the database connection.
  53. RTFM! PHP offers a fantastic manual, possibly one of the best out there, which makes it a very hands on language, providing working examples and talking in plain English. Please USE IT!

Here’s a short list of cool features that might have slipped under your radar as well:

  1. Use ip2long() and long2ip() to store IP addresses as integers instead of strings in a database. This will reduce the storage space by almost a factor of four (15 bytes for char(15) vs. 4 bytes for the integer), make it easier to calculate whether a certain address falls within a range, and speed-up searches and sorts (sometimes by quite a bit).
  2. Partially validate email addresses by checking that the domain name exists with checkdnsrr(). This built-in function checks to ensure that a specified domain name resolves to an IP address. A simple user-defined function that builds on checkdnsrr() to partially valid email addresses can be found in the user comments section in the PHP docs. This is handy for catching those occasional folks who think their email address is ‘joeuser@wwwphp.net’ instead of ‘joeuser@php.net’.
  3. If you’re using PHP 5 with MySQL 4.1 or above, consider ditching the mysql_* functions for the improved mysqli_* functions. One nice feature is that you can use prepared statements, which may speed up queries if you maintain a database-intensive website. Some benchmarks.
  4. Learn to love the ternary operator.
  5. If you get the feeling that you might be reinventing the wheel during a project, check PEAR before you write another line. PEAR is a great resource that many PHP developers are aware of, yet many more are not. It’s an online repository containing over 400 reusable snippets that can be dropped right into your PHP application. Unless your project is trully unique, you ought to be able to find a PEAR package that saves at least a little time. (Also see PECL)
  6. Automatically print a nicely formatted copy of a page’s source code with highlight_file().This function is handy for when you need to ask for some assistance with a script in a messageboard, IRC, etc. Obviously, some care must be taken not to accidently show your source when it contains DB connection information, passwords, etc.
  7. Prevent potentially sensitive error messages from being shown to users with the error_reporting(0) function. Ideally error reporting should be completely disabled on a production server from within php.ini. However if you’re on a shared webhost and you aren’t given your own php.ini, then your best bet is to add error_reporting(0); as the first line in each of your scripts (or use it with require_once().) This will prevent potentially sensitive SQL queries and path names from being displayed if things go awry.
  8. Use gzcompress() and gzuncompress() to transparently compress/decompress large strings before storing them in a database. These built-in functions use the gzip algorithm and can compress plaintext up to 90%. I use these functions almost everytime I read/write to a BLOB field within PHP. The only exception is when I need full text indexing capabilities.
  9. Return multiple values from a function with “by reference” parameters. Like the ternary operator, most PHP developers who come from a more formalized programming background already know this one. However, those who’s background is more HTML than Pascal, probably have wondered at one time “how do I get multiple values back from a function I wrote, even though I can only use one return value?” The answer is that you precede a variable with “&” and use it “by reference” instead of “by value”.
  10. Fully understand “magic quotes” and the dangers of SQL injection. I’m hoping that most developers reading this are already familiar with SQL injection. However, I list it here because it’s absolutely critical to understand. If you’ve never heard the term before, spend the entire rest of the day googling and reading.