Optimizing PHP string concatenation

Hello there,

This post is not really a question, but it could be useful to share some coding tips.

Here is the one I'de like to share with you. I'm gonna show you 4 examples to do the same thing, but only the last one will be the best.

$foo = 'John SMITH';

"Hello $foo, welcome on my website.";

"Hello " . $foo . " welcome on my website.";

'Hello ' . $foo . ' welcome on my website.';

'Hello ', $foo , ' welcome on my website.';

I'm sure you all know that echo '$foo' won't work, but still, I'm pretty sure that you use double quote to display a simple information. THIS IS BAD.

Well let's begin : The first one is bad (as well as the second) because using double quote forces php to scan the string to look for a substitution to be done (I mean a variable).

The second one is a little better, since php has no replacement to do.

The third one, is better because of simple quote, so that the language knows he can just send the text without processing, but the "bad" thing is the use of concatenation (dot operator, like in the second example).

The last one uses simple quote, and the coma operator. Why is this solution better?

Well, what happens when Using the third solution?

php creates a string, containing "Hello ", then it has to enlarge it, to put the content of foo variable ("John SMITH"), and then, enlarge it again to put " Welcome on my website." sentence. Then, echo can use this, to ... echo it :)

Whereas in the 4th one, the only thing to do for echo is to send "Hello ", then $foo's content, then " Welcome on my website." to the output, and that is all! Because echo just has to send the text, without creating a string that will have to be enlarged to contain the whole texte (that would have been concate, which has to be grown (because of concatenation) and then displayed.

I'll try to find back some benchmarks and put them here.

Fell free to comment or react, and excuse my english, this is not my mother thong.

Linux Commands You Need to Know as Web Developer

As web developer, somewhere along the line, you’ll meet unix or linux shell. For those who never use unix/linux before, it could be frustate to deal with shell command.

For a simple thing as list files in current directory to extract zip files. So here a quick list commands you probably need for your next journey. In my opinion, it won’t hurt you to learn some of linux shell commands. Take a quick look, and maybe you have familiar with some of them.

  • chmod - change permission of a file or directory

    Linux has something called permission on file or folder.

    r - Read permission. Permission to read a file or browse a folder

    w - Write permission. Permission to write to file or create new file in the folder

    x - Execute permission. Permission to execute file

    $ls -al
    -rw-r--r-- 1 gchandrasa gchandrasa 43 2009-02-15 15:00 sample.txt

    the first 3 characters after -, rw- belong to users permission.

    the second 3 characters r-- belong to group permission.

    the third 3 characters r-- belong to others user permission.

    One way to change this permission is using the number

    r = 4

    w = 2

    x = 1

    So if you want to change the sample.txt permission to others, give read and write permissions.

    r = 4 and w = 2, so rw = 4 + 2 = 6

    $chmod 646 sample.txt

    Give read and write permission to users, read permission to group, and read and write permission to others.

  • cd - change directory

    Use this command to change your current directory.

    Example : you need to change directory to Documents

    $ cd Documents

  • cp - copy files and directories

    Example : copy file index.php from directory a to directory b

    $ cp a/index.php b/

  • ls - list files in a directory

    Example : list files in current directory

    $ ls

    Example : list files in current directory, and show hidden files.

    $ ls -a

  • man - display manual for a command
    $ man mv

  • mkdir - create new directories
    $ mkdir newfolder

  • mv - move (rename) files

    Use this command to move your file(s) or to rename file(s).

    $ mv oldfolder newfolder

  • rm - remove files or directories.

    Delete all files with extension jpg in current directory.

    $ rm *.jpg

  • tar - The GNU version of the tar archiving utility

    Untar and extract file

    $ tar -xvf test.tar.bz2

  • unzip - list, test and extract compressed files in a ZIP archive

    Extract file zip

    $ unzip test.zip