10 Ways to inprove your PHP
Ok heres a list of ways to improve your PHP Programming..
1. Use the cheat sheets
When ever your programming in php and mysql you should always have a cheet sheet at hand!,
there are several out there for you to use as references here are just some.
- MySql
- http://www.addedbytes.com/cheat-sheets/download/mysql-cheat-sheet-v1.png
- PHP
- http://www.addedbytes.com/cheat-sheets/download/php-cheat-sheet-v1.png
- Regex
- http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/png/
- Mod Rewrite
- http://www.addedbytes.com/cheat-sheets/download/mod_rewrite-cheat-sheet-v2.png
- CSS
- http://www.addedbytes.com/cheat-sheets/download/css-cheat-sheet-v2.png
- HTML
- http://www.addedbytes.com/cheat-sheets/download/html-cheat-sheet-v1.png[/URL]
2. Know your comparison operators
You should make sure you know your comparison operators and should study them wisely, here are some links that will give you information on where to get to know!
- Main information source
- http://docs.php.net/manual/en/language.operators.comparison.php[/URL]
3. Short cut the else stament
Speed in programming is a big thing because you down want your user waiting several seconds for your page to load because as they browse your site the seconds they have waited overall will have a downside to your site.
Take this example of a normal IF / ELSE statement.
-
$x = 10;
-
}else{
-
$x = 5;
-
}
Now this looks like regular coding but by doing the following will remove the need for the else
[php]
$x = 5;
if( expression ){
$x = 10;
}
[/php]by setting the value of $x before the if statement, when the expression is run if it fails it will just stay at the default value of 5
[SIZE=5]4. Less {brackets}[/SIZE]
[SIZE=5][SIZE=2]When using statements such is the if statement theres no need to have the brackets the command will still execute as normal, this decreases the processing time of the statement and also decreases the size of your script[/SIZE]
[SIZE=5]Example with the brackets[/SIZE]
[/SIZE][php]
if($i == 10){
$i++;
}
[/php]
Same statement without brackets.
[php]
if($i == 10) $i++;
[/php]
[SIZE=5]
[SIZE=5]You can also use multiple if/else staments using the structure. for example[/SIZE]
[/SIZE][php]
if ($i == 10) $i++; else $i–;
if ($theScene != ‘dead’) echo ‘w00t’;
foreach ($girls as $girl)
echo ‘Heya ‘ . $girl . ‘ Fancy a drink?’;
[/php]
This method is used alot within the programming industry but Personally i think it makes your code look less attractive, but if you have the need for speed then use this method.
[SIZE=5]5. User Ternary Operators[/SIZE]
[SIZE=2]Ternary operators apply to if/else statements and is usually called “Short Hand” its kinda similar to No.4 “Less {Brackets}” act slightly like a function where it will return data into a variable if provided… examples of ternary operators follow.[/SIZE]
[SIZE=5]6. Unset your variables[/SIZE]
[SIZE=2]Unsetting you variabls will free memory in your php allowing other actions to run faster, you unset a variable/array usining the unset() function.. example follows.[/SIZE]
[PHP]
$myName = ‘Litewarez’;
echo $myname;
//Ok so now $myName is not needed throughtout the rest of the script so ill unset it
unset($myname);
[/PHP]
Note:
if you have a globalized variable in a function when you unset the variable it will only unset the local instance of that variable.. example below.
[PHP]
$myname = ‘Litewarez’;
function myName(){
global $myName;
unset($myName);
}
myName();
echo $myName;
[/PHP]
it will still echo Litewarez as only the variable within the function is destroyed but also remember to unset your variables inside functions aswell.
[SIZE=5]7. Using catching.[/SIZE]
[SIZE=2]on every script you should use catching/buffering to speed up your webstie speed, what catching does is create an internal catch of the current page and compresses it before senduing it to the web-browser.. meaning less data to be sent.
Also i prefer to use ob_start(‘ob_gzhandler’); instead of ob_start
what the gxhandler does is using gzip to compress the website data if the web-browser accepts gzip-deflate meaning that all web browsers are supported. example below…
[/SIZE]
[PHP]
//you need to call this function before you are going to be outputting data.. such as echo / print / var_dump etc
ob_start(‘ob_gzhandler’);
//No we can start doing stuff within outr script
if($Litewarez == ‘Good Programmer’){
for($i=0;$i<5;$i++){
echo ‘w00t\n’;
}
}
[/PHP]
//now the out put will be compressed and upto 5 times faster
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
![[Google]]( http://www.warezteacher.com/wp-content/plugins/easy-adsenser/google-light.gif)

June 19th, 2009 at 5:07 PM
[...] more here: 10 Ways to inprove your PHP Posted in PHP | Tags: cheat, linux-, months-again-, offering-free, php-and, programming, [...]