Results 1 to 10 of 21
Threaded View
-
22nd May 2009, 03:42 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.com10 Ways to improve 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
- PHP
- Regex
- Mod Rewrite
- CSS
- HTML
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
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.
PHP Code:if( expression ){
$x = 10;
}else{
$x = 5;
}
PHP Code:$x = 5;
if( expression ){
$x = 10;
}
4. Less {brackets}
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
Example with the brackets
PHP Code:if($i == 10){
$i++;
}
PHP Code:if($i == 10) $i++;
PHP Code:if ($i == 10) $i++; else $i--;
if ($theScene != 'dead') echo 'w00t';
foreach ($girls as $girl)
echo 'Heya ' . $girl . ' Fancy a drink?';
5. User Ternary Operators
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.
6. Unset your variables
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.
PHP Code:$myName = 'Litewarez';
echo $myname;
//Ok so now $myName is not needed throughtout the rest of the script so ill unset it
unset($myname);
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 Code:
$myname = 'Litewarez';
function myName(){
global $myName;
unset($myName);
}
myName();
echo $myName;
7. Using catching.
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...
PHP Code://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';
}
}
8. Using Concatenation.
When you are joinig variables and strings together the method most people use is
PHP Code:$User_Welcome = "Hello $user";
Here are some examples of concatenation!
PHP Code:
<?php
$Welcome_Start = 'Welcome';
$Username = 'Litewarez';
$Welcome_End = 'to your profile';
/*
This is the WRONG way to join them strings together!!!!
*/
$Welcome = "$Welcome_Start $Username $Welcome_End";
//OUTPUT: Welcome Litewarez to your profile
/*
This is the CORRECT way
*/
$Welcome = $Welcome_Start.' '.$Username.' '.$Welcome_End;
//OUTPUT: Welcome Litewarez to your profile
?>
9. Using Incremental Operators.
Now these are some operators that are widely used when dealing with numbers, but im going to show you the importance of POST / PRE incremental operators.
heres the 4 variations of these types of mathematical operators
- Increment
- $number++
- ++$number
- Decrement
- $number--
- --$number
the reason for this is the fact that if you use the POST method then the nuber will be incremented AFTER it has been return and if you use PRE method then the number will be incremented then return.. some examples below
PHP Code:<?php
$number = 5;
//POST
echo 'I should be 5 : '.$number. '<br />';
echo 'I should be 5 : '.$number++. '<br />'; // here iv'e started to increment it
echo 'I should be 6 : '.$number. '<br />';
echo 'I should be 6 : '.$number--. '<br />';
echo 'I should be 5 : '.$number--. '<br />';
echo '<br />';
$number = 5;
//PRE
echo 'I should be 5 : '.$number. '<br />';
echo 'I should be 6 : '.++$number. '<br />';
echo 'I should be 7 : '.++$number. '<br />';
echo 'I should be 6 : '.--$number. '<br />';
echo 'I should be 6 : '.$number. '<br />';
?>
10. Error Supressions
Ok we NEED to know about error supression when using functions.
if you have built a website and everything is going well and then you start seing an error on your site, well you dont want users to see this error because:- Looks Nasty
- Messes up your design
- * helps hackers gain information about your directory structure
Theres a few ways you can do this:
In your php.ini file you can take control of the error_repoerting settings and turn of all errors on your website
this is what the settings look like in your php.ini file and you can change these accordingly!
Code:error_reporting = E_ALL & E_NOTICE & E_STRICT
Possible Variations.
- E_ERROR => 'error'
E_WARNING => 'warning'
E_PARSE => 'parsing error'
E_NOTICE => 'notice'
E_CORE_ERROR => 'core error'
E_CORE_WARNING => 'core warning'
E_COMPILE_ERROR => 'compile error'
E_COMPILE_WARNING => 'compile warning'
E_USER_ERROR => 'user error'
E_USER_WARNING => 'user warning'
E_USER_NOTICE => 'user notice'
http://uk3.php.net/manual/en/functio...-reporting.php
Also the necxt way to change the error types is by using a function in you php script calle error_reporting().
heres an example of the usage
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
and the next way to do this is by adding the supresser to your functions witch will be the at symbol (@) before the Function so heres an example
PHP Code://Working Function
$data = @file_get_contents('http://www.somesite.com');
//Function that will cause an error
$data = @file_get_contents(1,4,'hello',helloooo);
so just take into mind that its best to set your php.ini other than using the at sysbol becuase if your debugging you site you dont want to go threw your script removing the @ sysbol from everywhere..
11. End-Credits
theres not really much to put in here apart from i hope you enjoy learning some PHP and comment like mad if you like.
My name: Litewarez, aka Cornetofreak
NOTE: yuou may copy this post from http:www.besthostingforums.com/ and post it where ever you feel like but please give credits to the original contributer.
litewarez Reviewed by litewarez on . 10 Ways to improve 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 Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
- MySql
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Improve your site performance, improve SEO
By NewEraCracker in forum Tutorials and GuidesReplies: 41Last Post: 21st Jan 2012, 01:51 PM -
Help us improve
By happyface4ever in forum Webmaster DiscussionReplies: 2Last Post: 3rd Feb 2011, 06:10 PM -
PLz Help Us to Improve and how
By ahsanmcsd in forum Site ReviewsReplies: 4Last Post: 6th Nov 2010, 01:33 PM -
Best ways to improve Google PR?
By Storming in forum Webmaster DiscussionReplies: 12Last Post: 6th Jan 2010, 09:55 AM
themaCreator - create posts from...
Version 3.45 released. Open older version (or...