PHP flush, sleep, and browsers…

If anything else doesn’t work, try this sample code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i &lt; ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

// All you need is 256 spaces first
echo str_repeat(" ", 256); ob_flush();
for($i=0;$i&lt;10;$i++)
{
   echo $i,' ';
   ob_flush();
   sleep(1);
}

PHP strip_tags, while keeping the inner text

function strip_only($str, $tags) {
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
    return $str;
}

$str = '<p style="text-align:center">Paragraph</p><strong>Bold</strong><br/><span style="color:red">Red</span><h1>Header</h1>';

echo strip_only($str, array('p', 'h1'));
echo strip_only($str, '<p><h1>');

Credit: Steven (php.net)

Test your variables!

When PHP throws you an error, there is always a reason. My dev box is set up to throw me everything it finds, and as I am working on a code that needs strong optimization, a question crossed my mind. Before I ask the question, here’s the (simplified) error-ing code: // Function is called many times (~1M) function doSomething($arrTest) { $strName = $arrTest[0]; $strType = $arrTest[1]; $strColor = $arrTest[2]; } $arrTest = array('banana', 'fruit'); doSomething($arrTest); Of course, $arrTest[2] doesn’t exist, and PHP, as friendly as it is, works perfectly, just throwing a notice.
Read more...

Symfony, performance, and High CPU usage

Server is slowing down, and noticing that your Symfony project is consuming enormous amount of CPU? Try this tweak: # factories.yml all: routing: class: sfPatternRouting param: cache: null **What it does:** This config disables the Symfony Routing Cache, which uses PHP serialize/unserialize functions, and can cause lots of CPU processing. In my case, a high server load of ~8 went under 1 with this simple tweak (on a dual core). Easy change, but hard to find in the docs.
Read more...

How to backup and restore your data with MysqlDump

Simple backup: From one database, to a simple sql file. mysqldump -u [username] -p [password] [databasename] > [backupfile.sql] Backup specific tables One database, several specified tables, to a single sql file. mysqldump -u [username] -p [password] [databasename] [table1 table2 ....] > [backupfile.sql] Backup multiple databases Multiple databases, to a single sql file. mysqldump -u [username] -p [password] --databases [database1 database2...] > [backupfile.sql] Backup all databases All databases found in connection, to a single file.
Read more...

Symfony directory’ variables

For the times you’ll need access to some of Symfony’s directories: Symfony dir [php] // root directory structure 'sf_cache_dir_name' = 'cache', 'sf_log_dir_name' = 'log', 'sf_lib_dir_name' = 'lib', 'sf_model_dir_name' = 'model', 'sf_web_dir_name' = 'web', 'sf_data_dir_name' = 'data', 'sf_config_dir_name' = 'config', 'sf_apps_dir_name' = 'apps', // global directory structure 'sf_app_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$sf_app, 'sf_model_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'model', 'sf_lib_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'lib', 'sf_web_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'web', 'sf_upload_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.'uploads', 'sf_base_cache_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$sf_app, 'sf_cache_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$sf_app.DIRECTORY_SEPARATOR.$sf_environment, 'sf_log_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'log', 'sf_data_dir' = $sf_root_dir.
Read more...

Get MySQL’s table sizes

Want to check your database size?

SELECT table_schema "Database",
 sum( data_length + index_length ) / 1024 / 1024 "Size (MB)",
 sum( data_free )/ 1024 / 1024 "Free (MB)"
FROM information_schema.TABLES
GROUP BY table_schema ;

This will work in MySQL 5.0.2 and up. For older versions, use:

SHOW TABLE STATUS

Retrieve custom columns in Symfony

Sometimes while retrieving data, you might not need all the columns, especially on a large database query result set.

Symfony, or actually Propel (1.3) allows you to chose what columns to retrieve, and skip the hydrate process:

// Custom column, on large subset
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(TitlePeer::ID);
$c->addSelectColumn(TitlePeer::TITLE);
$resTitleRS = TitlePeer::doSelectStmt($c);
// Loop through result and create custom array
while ($row = $resTitleRS->fetch(PDO::FETCH_ASSOC)) {
  $title_id = $row['ID'];
  $title_name = $row['TITLE'];
  $arrTitleDB[$title_id] = $title_name;
}