Click here to Skip to main content
15,880,796 members
Home / Discussions / Linux, Apache, MySQL, PHP
   

Linux, Apache, MySQL, PHP

 
GeneralRe: send data Pin
cjoki25-Mar-11 2:52
cjoki25-Mar-11 2:52 
GeneralRe: send data Pin
Elham M25-Mar-11 3:15
Elham M25-Mar-11 3:15 
GeneralRe: send data Pin
cjoki25-Mar-11 3:32
cjoki25-Mar-11 3:32 
GeneralRe: send data Pin
Elham M25-Mar-11 7:05
Elham M25-Mar-11 7:05 
GeneralRe: send data Pin
cjoki25-Mar-11 10:02
cjoki25-Mar-11 10:02 
GeneralRe: send data Pin
Elham M25-Mar-11 21:17
Elham M25-Mar-11 21:17 
GeneralRemove a File Using PHP Pin
Haroon Mughal23-Mar-11 2:35
Haroon Mughal23-Mar-11 2:35 
GeneralRe: Remove a File Using PHP Pin
Joan M30-Mar-11 7:48
professionalJoan M30-Mar-11 7:48 
GeneralPHP Tips By Google Master Pin
Haroon Mughal23-Mar-11 2:33
Haroon Mughal23-Mar-11 2:33 
Profile your code to pinpoint bottlenecks
Hoare's dictum states that Premature optimization is the root of all evil, an important thing to keep in mind when trying to make your web sites faster. Before changing your code, you'll need to determine what is causing it to be slow. You may go through this guide, and many others on optimizing PHP, when the issue might instead be database-related or network-related. By profiling your PHP code, you can try to pinpoint bottlenecks.
Upgrade your version of PHP
The team of developers who maintain the PHP engine have made a number of significant performance improvements over the years. If your web server is still running an older version, such as PHP 3 or PHP 4, you may want to investigate upgrading before you try to optimize your code.
Migrating from PHP 4 to PHP 5.0.x
Migrating from PHP 5.0.x to PHP 5.1.x
Migrating from PHP 5.1.x to PHP 5.2.x
Use caching
Making use of a caching module, such as Memcache, or a templating system which supports caching, such as Smarty, can help to improve the performance of your website by caching database results and rendered pages.
Use output buffering
PHP uses a memory buffer to store all of the data that your script tries to print. This buffer can make your pages seem slow, because your users have to wait for the buffer to fill up before it sends them any data. Fortunately, you can make some changes that will force PHP to flush the output buffers sooner, and more often, making your site feel faster to your users.
Output Buffering Control
Avoid writing naive setters and getters
When writing classes in PHP, you can save time and speed up your scripts by working with object properties directly, rather than writing naive setters and getters. In the following example, the dog class uses the setName() and getName() methods for accessing the name property.
class dog {
public $name = '';

public function setName($name) {
$this->name = $name;
}

public function getName() {
return $this->name;
}
}
Notice that setName() and getName() do nothing more than store and return the name property, respectively.
$rover = new dog();
$rover->setName('rover');
echo $rover->getName();
Setting and calling the name property directly can run up to 100% faster, as well as cutting down on development time.
$rover = new dog();
$rover->name = 'rover';
echo $rover->name;
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;
There's no reason to copy the variable above. 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);
}
Produces:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe")
Instead of using a loop, you can combine the data into a single database query.
$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);
Produces:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...
GeneralPHP performance tips Pin
Haroon Mughal23-Mar-11 2:32
Haroon Mughal23-Mar-11 2:32 
Questioncss and link styles Pin
Joan M22-Mar-11 4:59
professionalJoan M22-Mar-11 4:59 
AnswerRe: css and link styles Pin
Luc Pattyn22-Mar-11 5:25
sitebuilderLuc Pattyn22-Mar-11 5:25 
QuestionWhy do images are not shown? Pin
Joan M21-Mar-11 5:43
professionalJoan M21-Mar-11 5:43 
AnswerRe: Why do images are not shown? Pin
Luc Pattyn21-Mar-11 5:50
sitebuilderLuc Pattyn21-Mar-11 5:50 
GeneralRe: Why do images are not shown? Pin
Joan M21-Mar-11 6:10
professionalJoan M21-Mar-11 6:10 
GeneralRe: Why do images are not shown? Pin
Luc Pattyn21-Mar-11 6:22
sitebuilderLuc Pattyn21-Mar-11 6:22 
GeneralRe: Why do images are not shown? Pin
Joan M21-Mar-11 6:22
professionalJoan M21-Mar-11 6:22 
AnswerRe: Why do images are not shown? Pin
Luc Pattyn21-Mar-11 6:24
sitebuilderLuc Pattyn21-Mar-11 6:24 
GeneralRe: Why do images are not shown? Pin
Joan M21-Mar-11 6:47
professionalJoan M21-Mar-11 6:47 
GeneralRe: Why do images are not shown? Pin
Luc Pattyn21-Mar-11 6:52
sitebuilderLuc Pattyn21-Mar-11 6:52 
GeneralRe: Why do images are not shown? Pin
Joan M21-Mar-11 7:09
professionalJoan M21-Mar-11 7:09 
GeneralRe: Why do images are not shown? Pin
Luc Pattyn21-Mar-11 7:17
sitebuilderLuc Pattyn21-Mar-11 7:17 
QuestionRe: Why do images are not shown? Pin
Joan M21-Mar-11 7:30
professionalJoan M21-Mar-11 7:30 
AnswerRe: Why do images are not shown? Pin
Luc Pattyn21-Mar-11 7:43
sitebuilderLuc Pattyn21-Mar-11 7:43 
QuestionPHP vs ASP.net Pin
m_7tem20-Mar-11 11:05
m_7tem20-Mar-11 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.