Recent Updates Toggle Comment Threads | Keyboard Shortcuts

  • Missing in Your Code Documentation 

    Decisions.

    You comment your code, you log your commits and you document your database, architecture and other project stuff. But do you log and document your decisions?

    Why should i do that?

    You always should program for the next developer! So you have coding conventions, commit logs and comments in code for it. But even the cutest code can’t help the next developer to understand why you did the things you did. Commit logs are more useful in this context but usually very short and some data is missing.

    When i look at code from others, some questions are often in my mind:

    • Why he did it like that?
    • It looks like an error but works?
    • Maybe it was intendiert to work like this?
    • Someone surly thought about that?

    You cannot answer this questions alone, you walk over to the developer who made it and talk about the questions. Often he can’t really remember the topic and you hear answers like: “I am not certain but i think we did it like that because someone said something …”. Hmm okay, who is “someone” and what was the argument for or against it?

    If you can figure it out, good. But usually you have to discuss the whole topic again and make a “new” decision.
    You have to do so also if the developer is not available currently or he left the company.

    What is a decision in this context?

    It can be a technology decision like using frameworks or libraries. A decision in the area of software architecture or a simple coding decision. You every day make many of these, you can’t document all of them, but if you have to discuss it with one or more of your peers it’s worth to document the outcome.

    Often decisions have to do with business logic, usually your product manager have to document such things, but to be on the safe side you should write it down in your documentation too.

    How should i do that?

    Document positive and negative decisions, the yea and ney sayers and their arguments for or against the topic and of course the date when you decided it. Some examples for larger and smaler decisions in prosa:

    • 2014/02/03: Let’s add a field “productId” to database-table “User”. @DerFichtl, @TylerDurdan think it’s not a good idea but the alternative would be complicated and hard to implement.
    • 2014/02/03: Flatrate vs. PPV: We implement flatrate, PPV is a nice idea but to early in our situation. @MrBlack, @MrPink, @MrWhite are for it. @MrYellow is doubtful because of competitors have already PPV. @MrBrown knows it’s more risky but thinks PPV would be the better choice.

    If you like a more structured way to document such things, try a table.

    Date Decision Yea Ney
    2014/02/03 We make something really strange in this sprint!
    • It’s funny doing strange things. @MrBlack @MrWhite
    • No, wait a sprint, i am on vacation the next sprint. @MrBrown

    What happens then?

    Documenting things is only useful if you use it later for other decisions. It should be possible to find all your notes about a specific topic. If you discuss a new decision, look in your documentation if you have already talked about before, maybe it’s already clear what to do, maybe a new argument arise and you have to change your decision from past.

    DerFichtl 6:14 am on July 20, 2014 Permalink | Reply  

     
  • @mashable: 15 Power-Washing Porn Pics for Clean Satisfaction – http://t.co/YxaBQtgScu http://t.co/YcvuULEoSg

    DerFichtl 8:27 pm on July 7, 2014 Permalink | Reply  

     
  • Intermittent MySQL crashes with error “Fatal error: cannot allocate memory for the buffer pool” … The Solution: http://dba.stackexchange.com/questions/25165/intermittent-mysql-crashes-with-error-fatal-error-cannot-allocate-memory-for-t

    DerFichtl 8:24 pm on July 7, 2014 Permalink | Reply  

     
  • How to add some swap space to your Linux server … http://web3us.com/drupal6/how-create-web-site-handbook/fedora-or-redhat-server-tips/adding-swap-space-linux-without-rebooting

    DerFichtl 8:23 pm on July 7, 2014 Permalink | Reply  

     
  • Learning Go: Some Snippets … 

    … started to play a bit with go, the programming language from google. First step, trying to build a webapplication in go … first insights: it’s really fucking easy …

    Tutorial: http://golang.org/doc/articles/wiki/

    Load a config from json file:

    
    import (
        "fmt"
        "encoding/json"
        "os"
    )
    
    type Config struct {
        Port string
    }
    
    func loadConfig() (Config) {
        
        file, err := os.Open("conf.json")
        if err != nil {
            fmt.Println("Error: " + err)
        }
    
        config := Config{}
        jsonParser := json.NewDecoder(file)
        if err = jsonParser.Decode(&config); err != nil {
            fmt.Println("Error: " + err)
        }
    
        return config
    }
    
    

    The JSON file …

    
    {
        "port": "8080"
    }
    
    

    Simple Webserver

    
    package main
    
    import (
        "fmt"
        "net/http"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
    }
    
    func main() {
        http.HandleFunc("/", handler)
        http.ListenAndServe(":8080", nil)
    }
    
    

    Render HTML Templates

    
    import (
        "html/template"
    )
    
    func renderTemplate(w http.ResponseWriter, tmplName string, p *Page) {
        t, _ := template.ParseFiles("views/" + tmplName + ".html")
        t.Execute(w, p)
    }
    
    

    DerFichtl 6:18 pm on July 2, 2014 Permalink | Reply
    Tags: go, golang   

     
  • Global Javascript Error Logging with Google Analytics 

    Does my website really work for all users, browsers, versions? There are hundreds of possible combinations, it’s not possible to test all, but we can try to monitor all users and make errors visible you never would test for …

    Make a global error handler and push all errors as events to Google Analytics. In Analytics you can then create some analyses about errors on browser/version or browser/os. Or you just go through the list and fix the errors in the specific browsers.

    Here is the code for pushing javascript errors as Analytics events. To make it work you have to include the Analytics code-block in the website. This version is for classic Analytics, not for the new Universion Analytics.

     
    function logErrorToAnalytics(errorMsg, url, lineNumber, column) {
        if(! _gaq) { return false; }
     
        var category = 'error';
        var url = url;
     
        var error = errorMsg + ' (' + lineNumber;
        if(column) {
            error += ':' + column;
        }
        error += ')';
     
        _gaq.push(['_trackEvent', category, url, error]);
    }
     
    window.onerror = function(errorMsg, url, lineNumber, column, errorObj) {
        try {
            logErrorToAnalytics(errorMsg, url, lineNumber, column);
            // console.log(errorMsg, url, lineNumber, column, errorObj);
        } catch(ex) {}
        return true; // hide real error
    };

    Check if it work in Live => Events with category filter “error”. Don’t forget to write an error in your script … else you wont see one in Analytics :)

    Bildschirmfoto 2014-06-28 um 09.37.10

    DerFichtl 7:45 am on June 28, 2014 Permalink | Reply  

     
  • Elasticsearch and node.js – Getting Started Example 

    Here a simple example how to use elasticsearch with node.js …

    Add Document to Index

    With client.create you can add a document to your index, if no id is given than some id is generated for you.
    Submit ?title= and &tags= as GET params
    eg.: http://127.0.0.1:1337/create?title=A%20nice%20title&tags=some,nice,tags

    function create(query) {
    
        var title = query.title;
        var tags = query.tags.split(',');
        
        var D = new Date();
        var date = parseInt(D.getTime()/1000);
    
        client.create({
            index: 'myindex',
            type: 'mytype',
            // id: '1',
            body: {
                title: title,
                tags: tags,   
                published: true,
                published_at: date,
            }
        }, function (error, response) {
            // ...
        });
    
        eventEmitter.emit('doOutput', {message:'okay'});
    }
    

    Searching

    Search in your index with q= url parameter …
    eg.: http://127.0.0.1:1337/search?q=nice

    function search(query) {
    
        var q = '';
        if (query.q) {
            q = query.q;
        }
    
        client.search({
            q: q,
        }).then(function (body) {
            eventEmitter.emit('doOutput', {message:'okay', hits:body.hits.hits});
        }, function (error) {
            console.trace(error.message);
        });
    }
    

    Generating output on event

    Output your results or status messages …

    function output(data) {
    
        response.writeHead(200, {'Content-Type': 'text/html'});
    
        if(data.hits) {
            str = '';
            for(i=0; i<data.hits.length; i++) {
                var hit = data.hits[i];
    
                var D = new Date(hit._source.published_at*1000);
                var published = util.format('%s-%s-%s',
                    D.getFullYear(), D.getMonth(), D.getDate());
    
                str += util.format('%s - %s (Score: %s, ID: %s)',
                    hit._source.title, published, hit._score, hit._id);
            }
            response.end(str+'\n');
        } else {
            response.end(data.message+'\n');
        } 
    }
    

    The server …

    Require some stuff, start http server and simple routing.

    var util = require('util');
    var events = require('events');
    var eventEmitter = new events.EventEmitter();
    var http = require('http');
    var url = require('url');
    var elasticsearch = require('elasticsearch');
    var client = new elasticsearch.Client({
        host: 'localhost:9200',
        log: 'trace'
    });
    var response = null;
    
    eventEmitter.on('doOutput', output);
    
    
    http.createServer(function(req, res) {
    
        response = res;
        var parsedUrl = url.parse(req.url, true);
    
        if(parsedUrl.pathname == '/create') {
            create(parsedUrl.query);
        } else if(parsedUrl.pathname == '/search') {
            search(parsedUrl.query);
        }
    
    }).listen(1337, '127.0.0.1');
    
    console.log('Server running at http://127.0.0.1:1337/');
    
    

    More infos about elasticsearch.js here:

    More about eventEmitters here:

    DerFichtl 11:24 am on March 9, 2014 Permalink
    Tags: elasticsearch, lucene, , search   

     
  • WordPress Custom Image Field in 80 lines 

    You need nearly 80 lines to add a custom file field to post edit screen (and create screen). There are some situations where it’s necessary to have more than one post image, for example if you want to have a separate image of facebook sharing (og:image).

    The examples i found online are a bit complicated and often do more than just adding an image field, so here is the minimal code to do just add the field and save the image-url to post-meta.

    Adding the field to admin is still simple and done in bohuco_edit_post and bohuco_image functions. There are two actions used, one for edit and the other for creating new posts. The bohuco_image formats the html for the form field, for this example i made it very simple.

     
        function bohuco_edit_post($post_id) {
            add_meta_box('bohuco_customImage', 'Custom Image', 'bohuco_image', 'post', 'normal', 'high' );
        }
        add_action('load-post.php', 'bohuco_edit_post');
        add_action('load-post-new.php', 'bohuco_edit_post');
     
        function bohuco_image($post, $field) {
            wp_nonce_field(plugin_basename(__FILE__), '_wpnonce_bohuco');
     
            if (! $value = get_post_meta($post->ID, $field['id'], true)) {
                $value = '';
            }
     
            echo '<input type="file" name="'.$field['id'].'_upload" size="55" /><br />';
            if ($value) {
                echo '<img src="'.$value.'" />';
            }
        }

    Saving happens in bohuco_save_post where first the upload is done with build-in wordpress function wp_handle_upload, after that the url of the uploaded image is saved to a meta field (add_post_meta or update_post_meta). The overrides array holds some configuration for wp_handle_upload, the test_form must be false because it checks if the default upload action is used.

     
        function bohuco_save_post($postId) {
     
            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                return $postId;
            }
     
            if(isset($_FILES['bohuco_customImage_upload']['name'])
                && ! empty($_FILES['bohuco_customImage_upload']['name'])) {
     
                if (! wp_verify_nonce($_REQUEST['_wpnonce_bohuco'], plugin_basename(__FILE__))) {
                    die('Security check'); 
                }
     
                $overrides = array('test_form'=>false);
                $result = wp_handle_upload($_FILES['bohuco_customImage_upload'], $overrides);
     
                if(isset($result['error']) && ! empty($result['error'])) {
                    echo '<div class="error">'.$result['error'].'</div>';
                } else {
                    $imageFileLocation = $result['url'];
                    if (! update_post_meta($postId, 'bohuco_customImage', $imageFileLocation)) {
                        add_post_meta($postId, 'bohuco_customImage', $imageFileLocation, true);
                    }                
                }
            }
        }
        add_action('save_post', 'bohuco_save_post');

    This all works only if you first change the editor form enctype to multipart/form-data as you can see in add_post_enctype function. It’s a hack but it works, maybe there is a better method to change the post form tag?

     
        function add_post_enctype() {
            echo "<script type='text/javascript'>
                      jQuery(document).ready(function(){
                          jQuery('#post').attr('enctype','multipart/form-data');
                      });
                  </script>";
        }
        add_action('admin_head', 'add_post_enctype');

    Here is the whole example as little plugin, it should also work if you put it in a functions.php of a theme. I build it in wordpress 3.8 but should also work in older versions.

    /*
    Plugin Name: Custom Image Field
    Plugin URI: http://bohuco.net/blog
    Version: 1.0.0
    Author: @DerFichtl
    */
     
    if (is_admin()) {
     
        /**
         * Modify form enctype to support uploads
         */
        function add_post_enctype() {
            echo "<script type='text/javascript'>
                      jQuery(document).ready(function(){
                          jQuery('#post').attr('enctype','multipart/form-data');
                      });
                  </script>";
        }
        add_action('admin_head', 'add_post_enctype');
     
        /**
         * Add custom field to new post and edit post screen
         */
        function bohuco_edit_post($post_id) {
            add_meta_box('bohuco_customImage', 'Custom Image', 'bohuco_image', 'post', 'normal', 'high' );
        }
        add_action('load-post.php', 'bohuco_edit_post');
        add_action('load-post-new.php', 'bohuco_edit_post');
     
        /**
         * Format the field html and output image if present
         */
        function bohuco_image($post, $field) {
            wp_nonce_field(plugin_basename(__FILE__), '_wpnonce_bohuco');
     
            if (! $value = get_post_meta($post->ID, $field['id'], true)) {
                $value = '';
            }
     
            echo '<input type="file" name="'.$field['id'].'_upload" size="55" /><br />';
            if ($value) {
                echo '<img src="'.$value.'" />';
            }
        }
     
        /**
         * Do upload and save custom meta field 
         */
        function bohuco_save_post($postId) {
     
            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                return $postId;
            }
     
            if(isset($_FILES['bohuco_customImage_upload']['name'])
                && ! empty($_FILES['bohuco_customImage_upload']['name'])) {
     
                if (! wp_verify_nonce($_REQUEST['_wpnonce_bohuco'], plugin_basename(__FILE__))) {
                    die('Security check'); 
                }
     
                $overrides = array('test_form'=>false);
                $result = wp_handle_upload($_FILES['bohuco_customImage_upload'], $overrides);
     
                if(isset($result['error']) && ! empty($result['error'])) {
                    echo '<div class="error">'.$result['error'].'</div>';
                } else {
                    $imageFileLocation = $result['url'];
                    if (! update_post_meta($postId, 'bohuco_customImage', $imageFileLocation)) {
                        add_post_meta($postId, 'bohuco_customImage', $imageFileLocation, true);
                    }                
                }
            }
        }
        add_action('save_post', 'bohuco_save_post');
    }

    Things to improve would be: 1. Check if the uploaded file is an image, example works with all files allowed by wordpress. 2. Create a thumbnail of the image. 3. The save function is called twice if a new post is saved.

    DerFichtl 4:23 am on December 17, 2013 Permalink
    Tags: ,   

     
  • Do Some Things About Your WordPress Security … and Why 

    1. Install a Security Plugin

    screenshot-1

    That’s an easy one, install a plugin that checks your security settings and give you advice. For example “Better WP Security” tell you 20 things you should do to improve your WordPress security and you can do the most of them with just one mouse click.

    It also ads database backup feature, login lockouts and ssl features. Check it out!

    2. Delete Your Admin-User

    There is a user with the username “admin” on every WordPress. You should create a new user with another username and admin-privileges, after that delete the “admin”-user and transfer all posts to the new user.

    Why? Automatic hacking attempts depends on WordPress defaults, so many attacks assume that there is an admin-user and try to get his password and/or privileges. See also 5. “Make Things Complicated”.

    3. Prevent Random Login Attempts

    Look at your apache logs, you will usually find many login attempts with admin user to your blog. That’s automated brute-force attacks. You are quite secure now because you already removed your admin user but you can do even more. Add a second password inquiry to your login-screen with .htaccess. You have a two step login procedure on different software levels then.

    Add this lines at the top of your .htaccess

    AuthType Basic
    AuthName "Restricted Area"
    AuthUserFile /var/www/htpasswd/.htpasswd
    
    <Files "wp-login.php">
      require valid-user
    </Files>
    

    Of course, you have to create a .htpasswd file with a user and password … and don’t use the same password for both levels please.

    4. Check Your Salts and Keys

    Everybody is talking about salts, “Adobe has no salts”, “LinkedIn hashes are not salted” and so on. What all this security guys are talking about? A salt is a random and secret string you should add to your password hash-generation so it’s much harder to decrypt passwords with rainbow tables.

    In wp-config.php are 8 keys and salts, everyone of these should be a different random string. If there is something like “put your unique phrase here” … DO IT NOW! There is a salt generator from wordpress.org that makes it easy and secure.

    define('AUTH_KEY',         '+W)!0iS266I@0TfT9jA4;qUtc!6W.D}4A !MEDZL0J +5#(l|_c=t`$$&F');
    define('SECURE_AUTH_KEY',  'xN&y_3FlF4vtBXKXv+-x7z00 |spmsZX`Vt_bk]hie~U;OE/JiQcUO,5EL5-;%~I');
    define('LOGGED_IN_KEY',    'SFgSKKIb7ZNP%+E)FJ fKbz-%?z`,M1#@o^GY lay;$![@+->4nc|6+1-~57*Dan');
    define('NONCE_KEY',        '9-sa~o?7T&.|o_|KtUIQ)jyTSs!v+r&[|H9`+lb|#|T},],dF@_G+l,Y$2&lyJ]v');
    define('AUTH_SALT',        '/Js]Ck2|5|smh.r%G!vjD+s3B7y2ECps3nV?qa.3M|*0K?B-=ZA:PH%uaP_Pk?bo');
    define('SECURE_AUTH_SALT', 'Lo[Vcx5KAtIA|N9z]1ST_kLBYSr@-]^N96-UZaH1=SM]VQYJ)zVMHV|P`X:KY*8,');
    define('LOGGED_IN_SALT',   'l3Pf#4o:U-6[3_QDf~;Pfz(E@jlA{5fs-2t,v|h`bKZ& =sY= .|t+,+:A*g/#uG');
    define('NONCE_SALT',       '}<|(G ;p+vc/Y(jyfW*@9O|fK0qpZtKLw(47,M)0_J-.DYKA^]Q-gdUn|<+_Y^!h');
    

    If everything looks okay or you just created new salts ... nevertheless you should change it just a bit, add some random chars to it, just for the case.

    5. Make Things Complicated

    There are some things you can do that it gets very complicated to automatically attack your wordpress. For example you can change your database-table-prefix to something random (not wp_), rename your wp-content folder or hide your wp-login.php file. All this things are not so easy to do, they may break your blog, so you should use "Better WP Security" or other plugins for that.

    6. Check File Permissions

    Okay it's a bit of work, but you should check you file permissions and set it to less as possible. There is no need that your config is always writable or that all your plugin folders have 777 permissions. Remove write-permissions wherever it's possible.

    A hacker is looking for places where he can modify things, make it hard for him to find such places.

    7. Deactivate The Theme- And Plugin-Editor

    This one is a must! Deactivate the build-in WordPress file-editors under themes- and plugins-menu, the menu-point where you can edit php-files direct in your wordpress. If ever an attacker get into your wordpress he can modify all files where apache-user has write-permissions.

    Add the following line to wp-config.php

    define('DISALLOW_FILE_EDIT', true);
    

    8. Clean Up a Bit

    Delete everything you don't need! Old and unused plugins and themes, readme-files, wordpress sample config, old database dumps, uploaded zip files, old renamed files ... anything you don't need anymore. Move it to a folder, make a zip out of it for backup and then delete it.

    You don't need this stuff anymore and there is a chance that in future someone discover a vulnerability in one of these files. If this day comes it's maybe to late to delete it, so you should do it now.

    DerFichtl 8:52 pm on December 2, 2013 Permalink  

     
  • how boyer-moore search algorithm works … en.wikipedia.org/wiki/Boyer%E2%…

    7:52 pm on November 29, 2013 Permalink  

     
  • backup gmail account with fetchmail lifehacker.com/235207/geek-to…

    3:19 pm on November 28, 2013 Permalink  

     
  • how to find your stuff with linux find command bohuco.net/blog/2013/11/l… #linux #commandline #developer

    7:56 pm on November 26, 2013 Permalink  

     
  • Linux “Find” 101 for Developers 

    Finding things on disk is a very important task, not only for sys-admins. Thank god there is a “find” command on every linux prompt. Here are the most important find options suitable for web-developers and programmers who needs to find something on their project folder or document root.

    Linux Find Commandline Screenshot

    Find Command Features

    • Finding files, folders, symlinks
    • Search Recursive in subfolders
    • Filter by user, groups, permissions, date/time
    • Change output format of results
    • Piping results to other commands

    Find Files in Folders

    # find . -name "*.php"
    start searching in current directory, search for *.php files
    # find . -name "Dat?.*"
    Use wildcards like * and ? (files any extension, starting with “Dat” and then single wildcard char)
    # find . -iname "DaTe.php"
    search case insensitive (match Date.php, date.php, …)
    # find . -follow -name "*.php"
    follow symlinks into other directories
    # find . -name "\.*"
    find files starting with a dot (.svn, .htaccess)
    # find . -name "[A-Z]*.php"
    use shell patterns, find files starting with uppercase (Classes for example) use [a-z] for lowercase
    # find . -user www-data
    find files by files owner (a linux user)
    # find . -group www-data
    find files by files owner group
    # find . -perm 777
    find files and folder with all permissions
    # find . -mindepth 1 -maxdepth 2 -name "*.php"
    limit the search depth to subfolders
    # find . -type d
    limit search to directories (d) or files (f)
    # find . -name "*.php" -printf "%p %k KB\n"
    use printf for output formating (not available on OSX)
    # find /var/www -name "*.php" -size +1M
    find files by size (larger than 1 Megabyte)
    # find /var/www -mmin -60
    find files by modify date, -mmin -60 means within 60 minutes, +60 would mean: not in the last 60 minutes. use -mtime for days.
    # find /var/www -amin -60
    find files by access date/time

    More about find command

    1. Find man page
    2. Ubuntu help on find command
    3. 35 practical examples of linux find

    DerFichtl 7:45 am on November 23, 2013 Permalink
    Tags: , , , web development   

     
  • PHP Constants – Define, Use and Performance 


    Using constants is a good idea if you have values that never change. It makes code more secure because the values can’t be changed after defining and it makes code more readable. Here is all you should know about constants, some performance charts and a dirty secret.

    A constant is case-sensitive by default. But a good convention for naming constants is: Always uppercase and separate words with underscores. Constants can only hold scalar values like integers, strings, boolean and float … no arrays.

    define('DB_HOST', 'localhost');
    define('DB_PORT', '8080');
    
    // numbers works too if not at the beginning
    define('DB1_PORT', 8000);
    
    // get all defined constants (also php constants like: E_NOTICE)
    var_dump(get_defined_constants());
    
    // get user constants only
    $allConstants = get_defined_constants(true); 
    var_dump($allConstants['user']);
    

    Define and Use Constants

    You can use the define() function or the const keyword (since php 5.3) to create a constant. I usually use define() because the const keyword works only on the top level scope and not in if-statements or functions. Constants are global by default and therefor “bad” by nature (exception are class-constants we will see later).

    If a constant is defined you can’t remove or modify it … obvious it’s a constant.

    if (true) { 
        // this works 
        define('MY_CONST', 'my const');
    
        // Parse error ... not top level scope
        const MY_CONST = 'my const';
    }
    
    define('MY_CONST', 'try 1');
    define('MY_CONST', 'retry'); // Notice: Constant MY_CONST already defined in ...
    MY_CONST = 'wtf'; // Parse Error!
    

    If you can use the const statement do it, const is twice as fast than define() function.

    There is a constant() function if you don’t know the name of a constant, for example if you have to compute the constant-name. The constant() function is two-times slower as using a constant direct.

    Checking for Constants

    If you want to know if a constant is defined then use defined() function. If you don’t check first and just try to use a non existent constant you will get a: “Notice: Use of undefined constant”

    if (defined('MY_CONST')) { // don't forget the quotes
        echo MY_CONST;
    }
    

    Dirty Secret: Use of Undefined Constant …

    This is one of PHPs dirty secrets, if you try to use an undefined constant, php will create it on-the-fly and uses as value the name of the constant. IMHO you should never use this circumstance as a feature it throws also an php notice …

    “Notice: Use of undefined constant myDirtyConstant – assumed ‘myDirtyConstant’ in …”

    When this happens, it is usually an programming error, for example you forgot an dollar sign …

    // MY_DIRTY_CONSTANT is not defined
    if(! defined('MY_DIRTY_CONSTANT')) { 
        echo "Test: ".MY_DIRTY_CONSTANT; // ... but you can use it???
    }
    
    // Often happens in array-keys
    $array = array();
    $array[myKey] = "nice try"; // should be $myKey
    

    Class Constants

    Class constants are defined on top of the class with the const statement. You can use the constant with two colons like a static method or variable (without dollar).

    class User {
        const TABLE = 'user';
    
        function getAll() {
            $sql = sprintf('SELECT * FROM %s', self::TABLE);
        }
    }
    

    The only way to get all defined class constants is using the reflection class.

    $refl = new ReflectionClass('User');
    var_dump($refl->getConstants());
    

    CONSTANT vs. Variable

    Surprising for me: Constants are much slower than variables, look at following chart about defining and using a variable / constant:


    DerFichtl 7:33 pm on November 12, 2013 Permalink
    Tags:   

     
    • Greg 10:45 am on November 18, 2013 Permalink | Reply

      how often do you access a constant 10k times in a loop? and all for what? a 100% improvement in performance on a scale of hundredths or thousandths of a second?

      • MF 3:41 pm on November 24, 2013 Permalink | Reply

        thanks for reply … sure its a micro-benchmark and just for interest and not important in real life … nevertheless i was surprised, usually constants should be faster than variables.

  • Pioneers Festival 2013: John Biggs from Techcrunch [Video] 

    John Biggs from Techcrunch talks about …

    • open source / open hardware
    • liking Beer and a bartender robot
    • why finding schematics is fun and not dead
    • commandlines are not dead
    • killing black boxes
    • his self repair fail of an ipad mini
    • getting (not) rich with a raspberry pi bitcoin miner
    • makerbars and hackathons

    To be honest, until now i don’t know what about this talk was … but he is a funny guy … and bartender robots are really fucking cool …

    DerFichtl 8:40 pm on November 7, 2013 Permalink  

     
  • PHP String Replace Functions – Cheatsheet and Tutorial 


    PHP is famous for the simple and rich string modification methods. One important type of modification is to replace a string with another. Here are the possible methods, how to use it. At the end you will find a performance comparison over all the function and different input sizes.

    Simple str_replace

    str_replace('search string aka needle', 'replace with', 'in this string aka haystack');
    

    Basic Example

    $name = 'bill';
    $replaceName = 'steve';
    $string = 'bill is a genius.';
    
    $newString = str_replace($name, $replaceName, $string); // => steve is a genius.
    

    Get Replacement Count

    // ... via pass-by-reference variable
    str_replace('one','two','one one one', $count);
    echo $count; // = 3
    

    Use Arrays as $search and $replace

    $search = array('a','b');
    $replace = array('b','a');
    echo str_replace($search, $replace, 'ababab'); // => aaaaaa
    

    Insensitive str_replace Function

    // str_replace is case-sensitive ... str_ireplace not
    echo str_ireplace('A', 'b', 'aA'); // => bb
    

    Use Strings like Arrays

    $str = 'a string';
    $str[0] = 'A'; // change first char
    $str[strlen($str)] = '!'; // append a char
    

    Use substr_replace for Replacing Strings by Index

    $str = 'another string';
    
    // insert a string on position 8 (overwrite 0 chars)
    $str = substr_replace($str, 'nice ', 8, 0);
    
    // overwrite 5 chars on pos 8 with the new string
    $str = substr_replace($str, 'not nice ', 8, 5);
    

    strtr Single Byte Replacements

    echo strtr('this is a nice text', "t", "T"); // => This is a nice TexT
    
    // a bit strange but ... replaces char by char ... all x=>X, t=>T, e=>E
    echo strtr('this is a nice text', "xte", "XTE"); // => This is a nicE TEXT
    
    // use it with associative arrays
    echo strtr('this is a nice text', array("xt"=>"XT")); // this is a nice teXT
    

    Swiss Army Knife preg_replace

    echo preg_replace("/nice/", "very nice", "this is a nice text");
    
    // replace with limit
    echo preg_replace("/t/", "T", "this is a nice text", 1);
    
    // replace with no limit
    echo preg_replace("/t/", "T", "this is a nice text", -1);
    
    // replace with $count variable
    echo preg_replace("/t/", "T", "this is a nice text", -1, $count);
    
    // replace with arrays, returns array("bbbbbb","bbbbbbb")
    var_dump( preg_replace(array('/a/'), array('b'), array('ababab','bababa')) );
    

    It’s getting complicated now … preg_replace_callback

    // replace with callback, returns a with uppercase A
    preg_replace_callback('/a/', function($match){
        return strtoupper($match[0]);
    }, 'ababab');
    
    // use create_function if you want a slow version (see below)
    preg_replace_callback('/a/', create_function(
        '$matches',
        'return $matches[0];'),
    'ababab');
    

    “ereg_replace” is marked as deprecated and should not used anymore.

    Here is the performance overview over the different replace functions with different parameters, avoid strtr with assoc array and preg_replace_callback with create_function it’s really slow.

    How will the performance change if the data-size changes? Size matters but the count of replacements matters much more, many matches makes str_replace slow.

    Here is the code template for the str_replace performance tests. microtime_float comes from php.net help pages.

    $s = 'ababababab';
    $a = 'cdcdcdcdcd';
    $b = 'b';
    
    $start = microtime_float();
    for($i=0; $i<100000; $i++) {
        str_replace($a, $b, $s, $c);
    }
    echo "input 10b, search 10b, $c\t".round(microtime_float()-$start, 4)."\n";
    

    Do you know more or better methods to replace strings ... share it in comments!


    DerFichtl 9:20 pm on November 4, 2013 Permalink
    Tags:   

     
  • 5 free online tools for testing your website’s speed thenextweb.com/dd/2013/11/03/… via @push

    9:19 pm on November 3, 2013 Permalink  

     
  • I saw @davemcclure at #pioneers13 … the best #startup roaster in the world. #follow

    5:41 pm on November 3, 2013 Permalink  

     
  • #XBox One Spielechef … haben sie die Lektion wirklich gelernt … bin mir nicht so sicher. derstandard.at/1381370574144/… #microsoft

    5:30 pm on November 3, 2013 Permalink  

     
  • Interview mit @lillikoisser zum Thema Corporate Blogs lily.co.at/corporate-blog…

    2:30 pm on November 3, 2013 Permalink  

     
  • “TaDa! – Why Big Unveilings Dont Work” lnkd.in/btPHSN8 via @DavidSpinks @shiraabel #iterate #adapt #feedback

    10:52 am on November 3, 2013 Permalink  

     
  • fixing some bugs in my meme generator … memes-generator.net #fun #sideproject #memes

    10:01 am on November 3, 2013 Permalink  

     
  • PHP CLI Simple HTTP-Server 

    Use PHP without Apache in dev-environments is very simple since PHP 5.4
    DocumentRoot is the current directory or use the -t argument.

    php -S localhost:8000

    You can also provide a router-script …

    php -S localhost:8000 router.php

    PHP Docs

    DerFichtl 7:37 pm on November 2, 2013 Permalink  

     
  • Wie #buffer einen Hacker-Angriff in ein Happy End verwandeln konnte … netzwertig.com/2013/11/01/hac…

    DerFichtl 3:33 pm on November 1, 2013 Permalink  

     
  • German Startup Fails in 2013 golem.de/news/offline-s… #pioneers13

    DerFichtl 6:38 am on November 1, 2013 Permalink  

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel