There is nothing wrong here except your browser.....

2009/11/13

My PHP Code-Standards Part 1

Filed under: Tips & Tricks — Tags: , — admin @ 9:35 pm

Here is a tutorial on how to write a nice and clean PHP code.

Let us start with variables.

I use prefix for variables with camel notation. This makes it easy to see and check the right variable for its type.

  1. $strName = ‘ali’ ;
  2. $intCounter = 1;
  3. $arrData = array(‘a’=>‘ali’ , ‘i’=>‘is’ , ‘p’=>‘perfect’);
  4. $blnTrigger = true;
  5. $objMailSender = new mailSenderClass();

I use single quote for my data.

  1. $strMyNameIs = ‘Alireza’;

And I use double quotes for SQL or strings that I need to have a single quote inside.

  1. $strSQL = "SELECT * FROM person WHERE name LIKE ‘ali’ ";

I never use variable inside a double quote string. I break up the quote.

  1. $strCode = "some $bad code ";// WRONG AND UGLY
  2. $strNiceCode = ‘Very ‘ . $strData . ‘ is pasted here’ ;// NICE CODE

Never ever use short variables like $a or $temp or other lazy short names.
Use variable names that are understandable and describe what it contains.
And using a camelNotation makes it easy to read.
Never start with underscore or other kind of separator for names.

  1. $strReturn = ‘Nice code’;

Only use underscore for constants where you can not use tha camel notation.

  1. define(‘FOO_BAR’,‘It works!’);

And do not start with underscore. There are some other system variables and functions that starts with underscore like _get() or $_SESSION

2009/08/18

Swape Gallery Light

Filed under: Scripts — Tags: , , , — admin @ 8:40 pm

SGL is a php5 script to list pictures.
This script finds all the pictures in a directory under ./test_pic/ and makes menus based on directories.

All you have to do is putt your pictures in ./test_pic/ directory. Or edit $strPathToFiles = ‘./test_pic’; in sgl4.class.php file.

You can organize your pictures by category, by putting it under sub-directories.

The thumbs/ directory must be writable (CHMOD 755) to generate thumbnails automatically first time you visit the site. It will help loading your images faster.

HOW TO INSTALL

All you have to do is unzip the file.
Unpack the sgl4.php and configure the paths.

Putt your pictures under ./test_pic/ directory.
And don’t forget to chmod 755 thumbs directory.

SYSTEM REQUIRED.

Server with PHP5.

Download: sgl4

2008/02/19

favicon2png

Filed under: Scripts — Tags: , , — admin @ 9:22 pm

Here is a script to download the favicon.ico files from a website and save it as a png file.

You must have imagemagick installed on your server to convert the ico file to png.

  1.  
  2. <!— html code —->
  3. <form action="?" method="post">
  4.   <input id="site" name="site" type="text"
  5. value="<?php echo $_POST['site']; ?/>" />
  6.   <input type="submit" />
  7. </form>
  8.  
  9. < ?php
  10. //— favicon2png by Alireza Balouch @ swape.net 2008
  11.  
  12. if($_POST[’site’] != ){
  13.  
  14. //finding the hostname
  15. $host = parse_url($_POST[’site’]);
  16. $host = $host[‘host’];
  17. $host = explode(‘.’ , $host);
  18. $host = $host[count($host) -2];
  19. $filename = ‘img/’ . $host . ‘.png’;
  20.  
  21. if (!is_file($filename)){
  22. // getting the favicon
  23. $handle = fopen( $_POST[’site’] . ‘/favicon.ico’, "rb");
  24. $contents = stream_get_contents($handle);
  25. fclose($handle);
  26.  
  27. file_put_contents(‘fav.ico’ , $contents );
  28. // converting to png
  29. $StrExec = ‘/usr/local/bin/convert fav.ico -resize 24×24\> . $filename ;
  30. $ret = exec($StrExec);
  31. }
  32. echo ‘<img src="’ . $filename . ‘" />’ . $_POST[’site’];
  33. }
  34. ?>
Powered by WordPress. Design by Alireza Balouch. Top